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

Chang look n feel cause original KeyListener to be removed from ComboBoxEditor

    XMLWordPrintable

Details

    Description

      FULL PRODUCT VERSION :
      Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
      Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)


      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_10-b03)
      Java HotSpot(TM) Server VM (build 1.5.0_10-b03, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP Professional Service Pack 2
      Linux Ubuntu 6.01 Intel 32 Bits Edition
      Solaris 10

      A DESCRIPTION OF THE PROBLEM :
      After changing the look n feel of a JFrame, the ComboBoxEditor's original KeyListner will be removed.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Compile the following provided source code.

      2. Run in the console command prompt using "java -jar JavaApplication6.jar"

      3. Type 'a' on the combo box.

      4. "key pressed" message box appear. "key pressed" printed in console command prompt.

      5. Cliked on "Is Original Key Listener still there???", "YES" message box appear

      6. Click Menu->Window

      7. Typed 'a' on the combo box, nothing appear.

      8. Clicked on "Is Original Key Listener still there???", "NOPE:(" message box appear

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      "key pressed" message box will always be poped up, even the look n feel had changed.

      ComboBoxEditor should always using the original key event listener, even the look n feel had changed.
      ACTUAL -
      ComboBoxEditor no longer hooked to the original key event listener, after look n feel had changed.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /*
       * NewJFrame.java
       *
       * Created on April 29, 2007, 11:25 PM
       */

      package javaapplication6;

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

      /**
       *
       * @author doraemon
       */
      public class Main extends javax.swing.JFrame {
          
          /** Creates new form NewJFrame */
          public Main() {
              initComponents();
              createLookAndFeelMenuItem();
              
              keyListener = this.getKeyListener();
              
              this.jComboBox1.getEditor().getEditorComponent().addKeyListener(keyListener);
          }
          
          /** This method is called from within the constructor to
           * initialize the form.
           * WARNING: Do NOT modify this code. The content of this method is
           * always regenerated by the Form Editor.
           */
          // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
          private void initComponents() {
              buttonGroup1 = new javax.swing.ButtonGroup();
              jComboBox1 = new javax.swing.JComboBox();
              jButton1 = new javax.swing.JButton();
              jMenuBar1 = new javax.swing.JMenuBar();
              jMenu1 = new javax.swing.JMenu();

              setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
              jComboBox1.setEditable(true);

              jButton1.setText("Is Original Key Listener still there???");
              jButton1.addActionListener(new java.awt.event.ActionListener() {
                  public void actionPerformed(java.awt.event.ActionEvent evt) {
                      jButton1ActionPerformed(evt);
                  }
              });

              jMenu1.setText("Menu");
              jMenuBar1.add(jMenu1);

              setJMenuBar(jMenuBar1);

              javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
              getContentPane().setLayout(layout);
              layout.setHorizontalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                          .addGroup(layout.createSequentialGroup()
                              .addGap(132, 132, 132)
                              .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                          .addGroup(layout.createSequentialGroup()
                              .addGap(58, 58, 58)
                              .addComponent(jButton1)))
                      .addContainerGap(137, Short.MAX_VALUE))
              );
              layout.setVerticalGroup(
                  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addGroup(layout.createSequentialGroup()
                      .addGap(35, 35, 35)
                      .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 118, Short.MAX_VALUE)
                      .addComponent(jButton1)
                      .addGap(83, 83, 83))
              );
              pack();
          }// </editor-fold>

          private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
      // TODO add your handling code here:
              KeyListener[] listeners = this.jComboBox1.getEditor().getEditorComponent().getKeyListeners();
              
              for(KeyListener listener : listeners) {
                  if(listener == keyListener) {
                      JOptionPane.showMessageDialog(this, "YES");
                      return;
                  }
              }
              
              JOptionPane.showMessageDialog(this, "NOPE :(");
          }
          
          /**
           * @param args the command line arguments
           */
          public static void main(String args[]) {
              java.awt.EventQueue.invokeLater(new Runnable() {
                  public void run() {
                      new Main().setVisible(true);
                  }
              });
          }
          
          private java.awt.event.KeyListener getKeyListener() {
              return new KeyAdapter() {
                  public void keyPressed(KeyEvent evt) {
                      System.out.println("key pressed");
                      JOptionPane.showMessageDialog(Main.this, "key pressed");
                  }
              };
          }
          public void createLookAndFeelMenuItem() {
              LookAndFeel currentlaf = UIManager.getLookAndFeel();
              
              UIManager.LookAndFeelInfo[] lafInfo = UIManager.getInstalledLookAndFeels();

              for(int i=0; i<lafInfo.length; i++) {
                  JMenuItem mi = (JRadioButtonMenuItem) jMenu1.add(new JRadioButtonMenuItem(lafInfo[i].getName()));
                  buttonGroup1.add(mi);
                  mi.addActionListener(new ChangeLookAndFeelAction(this, lafInfo[i].getClassName()));
                  
                  if(currentlaf != null) {
                      if(lafInfo[i].getName() == currentlaf.getName())
                      {
                          ((JRadioButtonMenuItem) mi).setSelected(true);
                      }
                  }
              }
          }
          
          class ChangeLookAndFeelAction extends AbstractAction {
              Main mainFrame;
              String lafClassName;
        
              protected ChangeLookAndFeelAction(Main mainFrame, String lafClassName) {
                  super("ChangeTheme");
                  this.mainFrame = mainFrame;
                  this.lafClassName = lafClassName;
              }

              public void actionPerformed(ActionEvent e) {
                  mainFrame.setLookAndFeel(lafClassName);
              }
          }
          
          public void setLookAndFeel(String lafClassName) {
              try {
                  UIManager.setLookAndFeel(lafClassName);
                  SwingUtilities.updateComponentTreeUI(this);
              }
              catch(Exception exp) {
                  exp.printStackTrace();
              }
          }
          // Variables declaration - do not modify
          private javax.swing.ButtonGroup buttonGroup1;
          private javax.swing.JButton jButton1;
          private javax.swing.JComboBox jComboBox1;
          private javax.swing.JMenu jMenu1;
          private javax.swing.JMenuBar jMenuBar1;
          // End of variables declaration
          private KeyListener keyListener;
          
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      doesn't allow user the change the look n feel during run-time.

      Attachments

        Activity

          People

            dnguyen Damon Nguyen
            ryeung Roger Yeung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Imported:
              Indexed: