Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Programming challenge

  • 25-02-2004 5:05pm
    #1
    Closed Accounts Posts: 364 ✭✭


    Heres a problem thats been wrecking our head for almost a week. I have written a small and simple program to demonstrate it. Here is the error.

    Basically we want a JLabel to be updated with a list of numbers based on what Jbuttons are pressed. So, when a user clicks on the "1" button, the JLabel shows a "1", and this is set using setText(). When the user clicks on another JButton, say the "13" button, the JLabel will now show "1, 13", and so forth. The numbers clicked are added into an array of integers. An integer variable called tableIndex keeps tabs on the amount of numbers entered, and is incremented each time a button is pressed.

    One thing to note is that the JLabel and the JButtons are contained in a panel we call mainPanel, and there is a sidePanel used where the different areas of the application can be navigated. Clicking on the side buttons will cause the mainPanel to have its components removed, and the mainPanel will be revalidated and repainted.

    Our code works fine when the user enters clicks the buttons and the JLabel is updated, but once we click on the sidebar buttons to go to the other area of the application, and return, the numbers we press are added to the integer array twice, and the numIndex variable is updated twice.

    I have been trying to figure this out for a week, and I have been so far unsucessful. If anyone can please help I would be very greatful. Apologies for the verbosity, but I feel this information is important to include. Here is the code in queston.
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class BugHunt extends JFrame implements ActionListener 
    {
    	JLabel sideBar = new JLabel();
    	JLabel mainBar = new JLabel();
    
    	JButton addScreen = new JButton("Add items");
    	JButton viewScreen = new JButton("View items");
    
    	JButton printCount = new JButton("Get count");
    
    	JButton addOne = new JButton("1");
    	JButton addTwo = new JButton("2");
    	JButton addThree = new JButton("3");
    	JButton addFour = new JButton("4");
    	JButton addFive = new JButton("5");
    	JButton addSix = new JButton("6");
    	JButton addSeven = new JButton("7");
    	JButton addEight = new JButton("8");
    	JButton addNine = new JButton("9");
    	JButton addTen = new JButton("10");
    
    	JLabel numberView = new JLabel("Nothing added yet");
    	
    	int[] numsEntered = new int[10];
    	int numIndex = 0;
    	String numString = "";
    
    	public BugHunt(String title)
    	{
    		super(title);
    		setSize(500, 400);
    		setResizable(false);
    	}
    	public void setPanels()
    	{
    		Container content = getContentPane();
    		content.setLayout(null);
    
    		sideBar.setBounds(0, 0, 150, 400);
    		sideBar.setLayout(null);
    		content.add(sideBar);
    
    		mainBar.setBounds(150, 0, 350, 400);
    		mainBar.setLayout(null);
    		content.add(mainBar);
    	}
    	public void addSideBarComponents()
    	{
    		addScreen.setBounds(10, 10, 130, 40);
    		viewScreen.setBounds(10, 60, 130, 40);
    		
    		addScreen.addActionListener(this);
    		viewScreen.addActionListener(this);
    
    		sideBar.add(addScreen);
    		sideBar.add(viewScreen);
    	}
    	public void addMainComponents()
    	{
    		mainBar.removeAll();
    
    		addOne.setBounds(10, 10, 60, 20);
    		addTwo.setBounds(80, 10, 60, 20);
    		addThree.setBounds(150, 10, 60, 20);
    		addFour.setBounds(220, 10, 60, 20);
    		addFive.setBounds(290, 10, 60, 20);
    
    		addSix.setBounds(10, 40, 60, 20);
    		addSeven.setBounds(80, 40, 60, 20);
    		addEight.setBounds(150, 40, 60, 20);
    		addNine.setBounds(220, 40, 60, 20);
    		addTen.setBounds(290, 40, 60, 20);
    
    		printCount.setBounds(100, 100, 100, 30);
    		printCount.addActionListener(this);
    		
    		addOne.addActionListener(this);
    		addTwo.addActionListener(this);
    		addThree.addActionListener(this);
    		addFour.addActionListener(this);
    		addFive.addActionListener(this);
    
    		addSix.addActionListener(this);
    		addSeven.addActionListener(this);
    		addEight.addActionListener(this);
    		addNine.addActionListener(this);
    		addTen.addActionListener(this);
    
    		mainBar.add(addOne);
    		mainBar.add(addTwo);
    		mainBar.add(addThree);
    		mainBar.add(addFour);
    		mainBar.add(addFive);
    
    		mainBar.add(addSix);
    		mainBar.add(addSeven);
    		mainBar.add(addEight);
    		mainBar.add(addNine);
    		mainBar.add(addTen);
    		
    		mainBar.add(printCount);
    
    		mainBar.revalidate();
    		mainBar.repaint();
    	}
    	public void addViewComponents()
    	{
    		mainBar.removeAll();
    
    		numberView.setBounds(30, 30, 300, 20);
    		mainBar.add(numberView);
    		numberView.setText(numString);
    		mainBar.revalidate();
    		mainBar.repaint();
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		String action = e.getActionCommand();
    		if(action == "Add items")
    		{
    			addMainComponents();
    		}
    		if(action == "View items")
    		{
    			addViewComponents();
    		}
    		if(action == "Get count")
    		{
    			System.out.println(numIndex);
    		}
    		if(action == "1")
    		{
    			numsEntered[numIndex] = 1;
    			numIndex++;
    		}
    		if(action == "2")
    		{
    			numsEntered[numIndex] = 2;
    			numIndex++;
    		}
    		if(action == "3")
    		{
    			numsEntered[numIndex] = 3;
    			numIndex++;
    		}
    		if(action == "4")
    		{
    			numsEntered[numIndex] = 4;
    			numIndex++;
    		}
    		if(action == "5")
    		{
    			numsEntered[numIndex] = 5;
    			numIndex++;
    		}
    		if(action == "6")
    		{
    			numsEntered[numIndex] = 6;
    			numIndex++;
    		}
    		if(action == "7")
    		{
    			numsEntered[numIndex] = 7;
    			numIndex++;
    		}
    		if(action == "8")
    		{
    			numsEntered[numIndex] = 8;
    			numIndex++;
    		}
    		if(action == "9")
    		{
    			numsEntered[numIndex] = 9;
    			numIndex++;
    		}
    		if(action == "10")
    		{
    			numsEntered[numIndex] = 10;
    			numIndex++;
    		}
    	}
    	public static void main(String [] args)
    	{
    		BugHunt test = new BugHunt("A simple test");
    		test.setPanels();
    		test.addSideBarComponents();
    		test.show();
    	}
    }
    
    
    

    Thanks

    Matt


Comments

  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    I've just scanned over hte code, but my guess is that when you return from the sidebar you are assigning handlers a second time with a call to addMainComponents. As a result, the buttons have two handlers, each of which is calling the same routine, resulting in the number being added twice.

    If you go out to the sidebars and back a second time, and then start getting triple-entries, you'll know thats it.

    If I'm right, it should be easy to fix....there's no shortge of ways of coding around it.

    jc


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Seconding bonkey's opinion...

    When you hit addScreen (I assume is the button labeled "Add Components)", you are in fact adding a second ActionListener to each of the buttons. I'm quite sure that if you hit it a third time, you would find everything happening to you three times.


  • Closed Accounts Posts: 364 ✭✭Matfinn


    Bonkey

    I could kiss you. Thank you man!! :) Legen thanks

    We would never have gotten that.

    Doodle, thanks for your input aswell :)

    Ta


Advertisement