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

Value in text field of JComboBox disappears

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.2.1
    • client-libs
    • beta
    • x86
    • windows_nt



      Name: skT88420 Date: 06/30/99


      Under JDK 1.2 when the first call is made to the insertItemAt() method of a JComboBox from a custom focus manager the value disappears from the text field portion of the combo box. This does not happen under swing 1.0, nor does it happen for the second or subsequent calls to insertItemAt(), even under JDK 1.2. Here's a repro (build for JDK 1.2, run, enter a value in the combo box text field, and press the Enter key to observe the behavior).

      import java.awt.Component;
      import java.awt.event.*;
      // import com.sun.java.swing.*; // use for swing 1.0.x
      import javax.swing.*; // use for JDK 1.2

      /**
       * The <code>cb_bug</code> class is a repro case for a bug
       * introduced by JDK 1.2. In previous versions of swing
       * (1.0.x) the value in the text field portion of the combo
       * box remains in place after the value is added to the
       * combo box by the insertItemAt() method. With JDK 1.2 this
       * value disappears from the text field for the first call to
       * insertItemAt() if the call is made from a custom focus
       * manager. All subsequent calls after the first leave the
       * value in the text field intact for both versions of swing.
       */
      public class cb_bug extends JComboBox {
          public static void main(String[] av) {
              cb_bug box = new cb_bug();
              JButton button = new JButton("Search");
              JFrame frame = new JFrame("Combo Box bug");
              JPanel panel = new JPanel();
              panel.add(box);
              panel.add(button);
              frame.getContentPane().add(panel);
              box.setEditable(true);
              button.addActionListener(makeOkListener(box));
              FocusManager.setCurrentManager(box.makeFocusManager(button));
              frame.pack();
              frame.show();
          }

          static private ActionListener makeOkListener(JComboBox b) {
              final JComboBox cb = b;
              return new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                      String s = (String)cb.getEditor().getItem();
                      cb.insertItemAt(s, 0);
                  }
              };
          }
          private FocusManager makeFocusManager(JButton b) {
              return new SearchFocusManager(b);
          }
          private class SearchFocusManager extends DefaultFocusManager {
              JButton b;
              public SearchFocusManager(JButton button) {
                  super();
                  b = button;
              }
              public void processKeyEvent(Component c, KeyEvent e) {
                  if (e.getKeyCode() == KeyEvent.VK_ENTER)
                      if (e.getID() == KeyEvent.KEY_PRESSED)
                          b.doClick();
              }
          }
      }
      (Review ID: 85027)
      ======================================================================

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: