JComboBox width

别来无恙 提交于 2019-11-28 23:36:06
camickr

The width is automatically determined by the width of the largest item added to the combo box. You can control the display by using:

comboBox.setPrototypeDisplayValue("text here");

You might also consider using the Combo Box Popup to control the popup size.

Edit:

Since you added code that shows you are using a BoxLayout you can try the following:

comboBox.setMaximumSize( comboBox.getPreferredSize() );

Or you can do something like:

JPanel wrapper = new JPanel();
wrapper.add( comboBox );
panel.add( wrapper );

Read the section from the Swing tutorial on Using Layout Managers to understand how these suggestions work.

try comboBox.setPreferredWidth(200); or some other value to set the width

jzd is right. The actual API is setPreferredSize(new Dimension(...));

Use a different LayoutManager. Try FlowLayout.

Here is something you can do with box layout.

  • Change axis to line axis, Add
  • horizontal glue, Add rigid area,
  • place the component

. code snippet below:

panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
panel.add(Box.createHorizontalGlue());
panel.add(Box.createRigidArea(new Dimension(10, 0)));
panel.add(combo);
frame.getContentPane().add(BorderLayout.NORTH, panel);

You might want to use setSize() method.

combo.setSize(200, combo.getPreferredSize().height);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!