3

I have a JComboBox object in my JFrame which behaves exactly as expected. However, I'd like to make it editable so that users can input alternative values if necessary. Unfortunately, using setEditable(true) to enable editing causes its width to increase significantly. How can I allow editing of the JComboBox while keeping its width only as wide as the largest selectable item requires?

The JComboBox is in a JPanel with a FlowLayout, however I don't think this is relevant because changing the width of the JPanel does not affect the width of the JComboBox.

uneditable jComboBox editable jComboBox

Sam
  • 7,252
  • 16
  • 46
  • 65
Altay_H
  • 489
  • 6
  • 14
  • So far the only way I found to produce the desired behavior is: `comboBox.setPreferredSize(new java.awt.Dimension(50, 22));` – Altay_H Jul 25 '11 at 21:02
  • What do you mean "the only way". I gave you a solution that allows you to specify the number of characters you want to display, so you don't have to hardcode the actual dimension. – camickr Jul 26 '11 at 14:48
  • My apologies. When I initially tried your solution I was running `comboBox.setEditable(true)` before I ran `pack()` which was causing the code not to work. When I moved it after `pack()` your code worked as intended. I'm not sure why it makes a difference... – Altay_H Jul 27 '11 at 20:30

3 Answers3

5

JComboBox@setPrototypeDisplayValue("XXXXXXXXXXXXXX")

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I tried various values, but none of them had any effect on the width of the `JComboBox`. – Altay_H Jul 25 '11 at 15:21
  • @Altay_H http://stackoverflow.com/questions/3194958/fast-replacement-for-jcombobox-basiccomboboxui, http://stackoverflow.com/questions/5225025/how-to-apply-another-layoutmanager-to-a-jcombobox-multi-column-jcombobox-attemp, http://stackoverflow.com/questions/6242341/jcombobox-change-drop-down-popup, – mKorbel Jul 25 '11 at 16:10
3

A JTextField has a default preferred size. It appears that if this size is greater than the preferred rendering size, then the text field size is used. You can change the preferred size by specifying the the number of columns to display in the text field. So you need to manipulate the editor with code something like:

JComboBox comboBox = new JComboBox( ... );
comboBox.setEditable( true );
ComboBoxEditor editor = comboBox.getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();
textField.setColumns(3);
camickr
  • 321,443
  • 19
  • 166
  • 288
2

Hmmm... I can't prompt this behavior on my Mac, but you might try creating/adding the combobox, then call

Dimension size = box.getSize();
box.setEditable(true);
box.setSize(size); // or box.setMaximumSize(size);
Alex Churchill
  • 4,887
  • 5
  • 30
  • 42
  • `box.setSize(size)` does not permanently change the width of the jComboBox for me. As soon as `pack()` or `setVisible(true)` occurs the size of box returns to its previous value. Even `setMaximumSize(size)` and `setMinimumSize(size)` are ignored. – Altay_H Jul 22 '11 at 20:26