16

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 adding the code here:

import javax.swing.*;
import java.awt.BorderLayout;

public class Window8  {

    JFrame frame;
    JPanel panel;
    JComboBox combo;
    public void go(){

    String[] option = { "STUDENT", "TEACHER" };

    combo.setPreferredSize(new Dimension(1,25));
    combo = new JComboBox(option);
    menu.setSelectedIndex(0);

    frame = new JFrame("DELETION"); 
    frame.setLocationRelativeTo(null);
    frame.setSize(400, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    panel = new JPanel();
    panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));

    frame.getContentPane().add(BorderLayout.NORTH,panel);
    panel.add(combo);   
}
Steve McLeod
  • 51,737
  • 47
  • 128
  • 184
Suman.hassan95
  • 3,895
  • 8
  • 26
  • 24

6 Answers6

28

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.

Electrons_Ahoy
  • 36,743
  • 36
  • 104
  • 127
camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    Using `setPrototypeDisplayValue` with a sensitive option helps on properly resizing the JComboBox while allowing it to have a different size according to container definition. Thanks! – Alfabravo Nov 16 '17 at 22:02
4

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

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

Romain Hippeau
  • 24,113
  • 5
  • 60
  • 79
1

You might want to use setSize() method.

combo.setSize(200, combo.getPreferredSize().height);
Ned
  • 1,055
  • 9
  • 34
  • 58
1

Use a different LayoutManager. Try FlowLayout.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • The width will be automatically determined based on the contents or use setPreferredSize() or camickr's suggestion. But if you put it in a layout that stretches it will not matter. – jzd Jan 07 '11 at 19:25
1

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);
ring bearer
  • 20,383
  • 7
  • 59
  • 72
-1

Don't use JCombobox<>() instead of that use Combobox<>() then width won't change, also if you set preferred size it won't change with Combobox so use this instead