JComboBox width

后端 未结 5 961
南旧
南旧 2020-12-15 08:31

I have created a jComboBox but it takes the full width of the frame. how to set the width fixed.

yes borderlayout for the frame and box layout for the panel. i am

5条回答
  •  -上瘾入骨i
    2020-12-15 08:42

    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.

提交回复
热议问题