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

Java Swing actionlistener and JComboBox

Options
  • 18-02-2004 3:22pm
    #1
    Closed Accounts Posts: 364 ✭✭


    Hi

    Im having a small problem with a GUI application im trying to code using the Java Swing windowing toolkit.

    I have a load of buttons and they all have ActionListeners attached to them. I have one JComboBox and what I want to do with that is to update a JLabel I have based on the selection made from the ComboBox. My code goes roughly like this.

    public void actionPerformed(ActionEvent e)
    {
    //we have this code to handle button presses
    String action = e.getActionCommand();
    if(action.equals("Button Name");
    {
    do this......
    }

    //and then theres this to handle the JComboBox
    JComboBox act = (JComboBox)e.getSource();
    String selected = (String)act.getSelectedItem();
    sel.setText("You chose: " + selected);
    }

    The thing is we get a load of RunTime errors. Anyone ever seen this before. Any help appreciated, thanks

    Matt


Comments

  • Registered Users Posts: 1,990 ✭✭✭lynchie


    What runtime error are you getting? Post the stack trace and the piece of code where the error is being thrown


  • Closed Accounts Posts: 364 ✭✭Matfinn


    I solved it today, but thanks for replying all the same. Heres how I did it....

    I had to make a new class with an actionPerformed method in it. This class would put in the ActionListener for the JComboBox
    public class comboWatch implements ActionListener
    {
          public void actionPerformed(ActionEvent e)
          {
                code....
          }
    }
    
    //The in my main class I would have 
    
    JComboBox datelist = new JComboBox(String array);
    datelist.addActionListener(new comboWatch());
    
    I needed a seperate ActionListener because the first ActionListener with the buttons was interfering with the JComboBox ActionListener and thats why I had a load of runtime errors.

    Thanks anyway

    Matt


  • Registered Users Posts: 1,990 ✭✭✭lynchie


    You can still use one actionPerformed method.

    All you have to do is query the getSource method for the type of object that cause the ActionEvent

    e.g.
    public void actionPerformed(ActionEvent e)  
    {
        	Object o = e.getSource();
            if(o instanceof JComboBox)
      	{
      		JComboBox combo = (JComboBox) o;
      		//do all combobox related stuff here...
      		...
      	}
        	if(o instanceof JButton)
      	{
      		JButton button = (JButton) o;
      		//do all button related stuff here...
      		...
      	}
        	...
    }
    


Advertisement