Results 1 to 4 of 4
  1. #1
    Plissken is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default Serious Problem with JComboBox

    When the model of a combo box is changed by setting it to a certain index, usually using setSelectedIndex() or setSelectedItemAt(int index), the software (code) calls actionPerformed. This results in multiple stack calls to actionPerformed, that then unwind. This is undesirable if you have other code in actionPerformed that should only be go through once per actionPerformed call. Essentially combobox is like a recursive function.

    How can you prevent this?

    Here's more detail:

    actionPerformed()
    {
    ....//code
    if ( event.getSource() == monthBox || event.getSource == yearBox )
    {
    ....//do this
    }
    }

    So every time setSelectedIndex or setSelectedItem is used on a combo box, it calls actionPerformed multiple times before even finishing the first original actionPerformed call.
    Last edited by Plissken; 02-02-2010 at 02:14 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I see no recursion in my small demo program that tests ItemListener and ActionListener:

    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.util.Random;
    
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class ComboBoxListenerTest {
       private static void createAndShowUI() {
          final String[] items = {"Sun", "Mon", "Tues", "Wed", 
                   "Thurs", "Fri", "Sat"};
          final DefaultComboBoxModel comboModel = new DefaultComboBoxModel(items);
          final JComboBox comboBox = new JComboBox(comboModel);
          final Random random = new Random();
          
          comboBox.addItemListener(new ItemListener() {
    
             public void itemStateChanged(ItemEvent e) {
                System.out.println("ItemListener triggered. Item: " + e.getItem());
             }
             
          });
          comboBox.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                System.out.println("ActionListener triggered. Item: " + comboBox.getSelectedItem());
             }
          });
          
          JButton changeModel = new JButton("Change Model");
          changeModel.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                int i = random.nextInt(comboModel.getSize());
                comboBox.setSelectedIndex(i);
             }
          });
          
          JPanel panel = new JPanel();
          panel.add(comboBox);
          panel.add(changeModel);
          
          JFrame frame = new JFrame("ComboBoxListenerTest");
          frame.getContentPane().add(panel);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    Perhaps you should post a compilable program that demonstrates the issue.

  3. #3
    Plissken is offline Member
    Join Date
    Feb 2010
    Posts
    4
    Rep Power
    0

    Default

    The demo code you've written (thanks for taking the time to do that) works fine because there is a separate new actionPerformed for each component instead of one actionPerformed function for all components.

    I've solved the problem though. I used a boolean as a switch to suppress the calls to actionPerformed.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Plissken View Post
    The demo code you've written (thanks for taking the time to do that) works fine because there is a separate new actionPerformed for each component instead of one actionPerformed function for all components.I've solved the problem though. I used a boolean as a switch to suppress the calls to actionPerformed.
    which is better than having a "Switchboard" ActionPerformed. I suggest you do the same.

Similar Threads

  1. Replies: 2
    Last Post: 12-10-2009, 01:58 PM
  2. Problem with Jcombobox in Tablecell
    By yernikumar in forum AWT / Swing
    Replies: 1
    Last Post: 03-19-2009, 08:00 AM
  3. Accessibility of JComboBox problem
    By abedules78 in forum AWT / Swing
    Replies: 0
    Last Post: 12-26-2008, 01:11 PM
  4. I need help with JComboBox
    By eva21 in forum New To Java
    Replies: 1
    Last Post: 11-28-2008, 10:05 PM
  5. Problem with JComboBox and Jlist
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 05-07-2008, 08:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •