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

JComboBox editor borders inflate under certain circumstances

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version "1.6.0_10-beta"
      Java(TM) SE Runtime Environment (build 1.6.0_10-beta-b25)
      Java HotSpot(TM) Client VM (build 11.0-b12, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      See http://forums.sun.com/thread.jspa?threadID=5319606&tstart=15 for details.

      If you put up a JComboBox, and change it from non-editable to editable, everything looks OK. If you then edit the contents of the box (with a custom ComboBoxEditor installed) and change the model as a result, the JComboBox abruptly changes sizes.

      This appears to be some sort of problem with the way that Insets are calculated around custom editors.

      I believe that the model change is causing the display size of an editable combobox to be computed wrongly.

      The problem occurs under at least the Metal and Nimbus look and feels; it does not occur under the JGoodies Plastic look and feel.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please see the attached unit test.

        To reproduce, launch the test case. Check the checkbox to make the combobox editable. Observe that no size change takes place (as expected). Now type in some sort of text that does not initially appear in the model (like "foo"). Press return to activate the editor's built-in ActionListener, which inserts "foo" into the model. This model change will cause the JComboBox to "inflate" under Nimbus and Metal (at least) look and feels.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The JComboBox should stay the same size, whether it is editable or not.
      ACTUAL -
      Under certain circumstances, the combobox gets bigger (there is more padding around the editor component).

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package bugtest;
       
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.*;
       
      import org.junit.Test;
       
      public class TestCaseComboBoxEditorBug {
       
        @Test
        public void testComboBox() throws Exception {
          final Runnable runnable = new Runnable() {
              @Override
                public final void run() {
                final JComboBox comboBox = new JComboBox(new DefaultComboBoxModel(new String[] { "first", "second", "third", "fourth" }));
                final ComboBoxEditor comboBoxEditor = new ComboBoxEditor() {
       
                    private final JTextField editor = new JTextField();
       
                    @Override public final void selectAll() { editor.selectAll(); }
                    @Override public final void addActionListener(final ActionListener listener) { this.editor.addActionListener(listener); }
                    @Override public final void removeActionListener(final ActionListener listener) { this.editor.removeActionListener(listener); }
                    @Override public final void setItem(final Object item) { editor.setText(String.valueOf(item)); }
                    @Override public final Object getItem() { return editor.getText(); }
                    @Override public final Component getEditorComponent() { return editor; }
                  };
                comboBox.setEditor(comboBoxEditor);
                comboBoxEditor.addActionListener(new ActionListener() {
                    @Override
                    public final void actionPerformed(final ActionEvent event) {
                      if (comboBox.isEditable()) {
                        final Object selectedItem = comboBox.getSelectedItem();
                        if (selectedItem != null && comboBox.getSelectedIndex() < 0) {
                          ((MutableComboBoxModel)comboBox.getModel()).addElement(selectedItem);
                        }
                      }
                    }
                  });
                final Action setEditableAction = new AbstractAction("Editable") {
                    @Override
                    public final void actionPerformed(final ActionEvent event) {
                      comboBox.setEditable(((JCheckBox)event.getSource()).isSelected());
                    }
                  };
                final JCheckBox checkBox = new JCheckBox(setEditableAction);
                final JDialog dialog = new JDialog((Frame)null, true);
                final Container contentPane = dialog.getContentPane();
                contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
                contentPane.add(comboBox);
                contentPane.add(checkBox);
                dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                dialog.pack();
                dialog.setVisible(true);
              }
            };
          if (EventQueue.isDispatchThread()) {
            runnable.run();
          } else {
            EventQueue.invokeAndWait(runnable);
          }
        }
       
        public static final void main(final String[] ignored) throws Exception {
          new TestCaseComboBoxEditorBug().testComboBox();
        }
       
      }

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

            peterz Peter Zhelezniakov
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: