Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4298197

JComboBox fires extra action event on deselection

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.2
    • client-libs
    • x86
    • windows_98, windows_nt



      Name: skT88420 Date: 12/09/99


      java version "1.2.2"
      Classic VM (build JDK-1.2.2-W, native threads, symcjit)

      I have a JComboBox which for which I need to monitor for selection/deselection
      status. When a selection changes, I wish to see one event for the change, not
      individual events for the deselection of one item and the selection of another.
      Therfore, I would like to use an ActionEventListener for the JComboBox rather
      than an ItemEventListener.

      When I add an action listener, I get an event each time the selection changes
      (as expected). When I deselect programatically (by doing a setSelectedIndex(-1),
      for example), I get a single action event, also as expected.

      The problem is when I deselect via the GUI component. If I deselect by
      a) clicking the JComboBox to expose its pulldown list, and
      b) clicking on a selection from the pulldown list while holding down CTRL
      In this case I appear to get 2 action events that appear to contain the same
      information.

      It doesn't seem to me that JComboBox should be firing multiple action events in
      this case...

      A sample source file:

      =======

      import javax.swing.*;
      import java.awt.event.*;
      public class DeselectionTest extends JFrame
      {
        public DeselectionTest()
        {
          super("Combo Box Deselection Test");
          String[] items = {"One", "Two", "Three", "Four"};
          JComboBox box = new JComboBox(items);
          box.addActionListener(new SelectionListener());
          getContentPane().add(box);
        }


        public static void main(String[] args)
        {
          DeselectionTest test = new DeselectionTest();
          test.pack();
          test.setVisible(true);
        }

        private class SelectionListener implements ActionListener
        {
          public void actionPerformed(ActionEvent anEvent)
          {
            System.out.println("Received action event: " + anEvent.toString());
          }
        }
      }
      (Review ID: 98803)
      ======================================================================

      Name: krT82822 Date: 12/30/99


      C:\jdk1.2.2>java -version
      java version "1.2"
      Classic VM (build JDK-1.2-V, native threads)


      The JComboBox will fire two ActionPerformed Events instead of just one after
      changing the current selection in the combobox dropdown list. This situation
      will occur under these situations:
      - Motif PLAF is used
             AND
      - selection change is done using the mouse

      This situation does not occur if:
      - Windows or Java (Metal) PLAF is used
             OR
      - selection change is done using the up or down arrow keys on keyboard

      Here is code that can be used to demostrate the problem.

      import java.awt.*;
      import javax.swing.*;
      import java.awt.event.*;

      public class JComboTest extends JPanel {
          JComboBox jComboBox1 = new JComboBox();
          JButton btnMetal = new JButton();
          JButton btnWindows = new JButton();
          JButton btnMotif = new JButton();
          JScrollPane jScrollPane1 = new JScrollPane();
          JList jList1 = new JList(new DefaultListModel());
          JButton btnClear = new JButton();

          public JComboTest() {
              try {
                  jbInit();
              }
              catch(Exception ex) {
                  ex.printStackTrace();
              }
          }

          private void jbInit() throws Exception {
              this.setLayout(null);
              jComboBox1.setEditable(true);
              jComboBox1.setBounds(new Rectangle(10, 63, 267, 25));
              String item = "Item at ";
              for (int i = 0; i < 11; i ++){
                  jComboBox1.addItem (item + i);
              }
              jComboBox1.addActionListener(new java.awt.event.ActionListener() {

                  public void actionPerformed(ActionEvent e) {
                      jComboBox1_actionPerformed(e);
                  }
              });
              btnMetal.setText("Metal");
              btnMetal.setBounds(new Rectangle(300, 15, 87, 23));
              btnMetal.addActionListener(new java.awt.event.ActionListener() {

                  public void actionPerformed(ActionEvent e) {
                      btnMetal_actionPerformed(e);
                  }
              });
              btnWindows.setText("Windows");
              btnWindows.setBounds(new Rectangle(300, 44, 88, 23));
              btnWindows.addActionListener(new java.awt.event.ActionListener() {

                  public void actionPerformed(ActionEvent e) {
                      btnWindows_actionPerformed(e);
                  }
              });
              btnMotif.setText("Motif");
              btnMotif.setBounds(new Rectangle(301, 71, 88, 21));
              btnMotif.addActionListener(new java.awt.event.ActionListener() {

                  public void actionPerformed(ActionEvent e) {
                      btnMotif_actionPerformed(e);
                  }
              });
              jScrollPane1.setBounds(new Rectangle(301, 116, 221, 164));
              btnClear.setText("Clear List");
              btnClear.setBounds(new Rectangle(414, 70, 89, 22));
              btnClear.addActionListener(new java.awt.event.ActionListener() {

                  public void actionPerformed(ActionEvent e) {
                      btnClear_actionPerformed(e);
                  }
              });
              this.add(jComboBox1, null);
              this.add(btnMetal, null);
              this.add(btnWindows, null);
              this.add(btnMotif, null);
              this.add(jScrollPane1, null);
              this.add(btnClear, null);
              jScrollPane1.getViewport().add(jList1, null);

          }

          public static void main (String args[]){
          try{
           UIManager.setLookAndFeel
      ("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
          }catch(Exception e){
                  System.out.println("UIManager exception" + e.toString ());
              }
              javax.swing.JFrame frame = new javax.swing.JFrame();
              JComboTest panel = new JComboTest();
              frame.getContentPane().add(panel);
              frame.setSize(550,300);
              frame.setVisible(true);
              frame.addWindowListener(new java.awt.event.WindowAdapter() {
                  public void windowClosing(java.awt.event.WindowEvent we) {
      System.exit(0); }
              });
          }

          void jComboBox1_actionPerformed(ActionEvent e) {
              DefaultListModel listModel = (DefaultListModel)jList1.getModel ();
              listModel.addElement("JCombo1 action performed occured");
          }

          void btnMetal_actionPerformed(ActionEvent e) {
              try{
                  UIManager.setLookAndFeel
      ("javax.swing.plaf.metal.MetalLookAndFeel");
                  SwingUtilities.updateComponentTreeUI (this);
              }
              catch (Exception e1){
                  System.out.println(e1.toString ());
              }
          }

          void btnMotif_actionPerformed(ActionEvent e) {
              try{
                  UIManager.setLookAndFeel
      ("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                  SwingUtilities.updateComponentTreeUI (this);
              }
              catch (Exception e2){
                  System.out.println(e2.toString ());
              }
          }

          void btnWindows_actionPerformed(ActionEvent e) {
              try{
                  UIManager.setLookAndFeel
      ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                  SwingUtilities.updateComponentTreeUI (this);
              }
              catch (Exception e1){
                  System.out.println(e1.toString ());
              }
          }

          void btnClear_actionPerformed(ActionEvent e) {
              DefaultListModel lm = (DefaultListModel)jList1.getModel();
              lm.clear ();
          }
      }
      (Review ID: 99447)
      ======================================================================

            mdavidsosunw Mark Davidson (Inactive)
            skonchad Sandeep Konchady
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: