JComboBox width
Asked Answered
S

6

17

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);   
}
Sayre answered 7/1, 2011 at 18:37 Comment(2)
i have added my code. please have a look at it.Sayre
Reformatted code; copyedited; please revert if incorrect.Anemochore
K
31

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.

Korrie answered 7/1, 2011 at 18:42 Comment(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!Fenrir
G
4

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

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

Gefell answered 7/1, 2011 at 18:42 Comment(2)
i tried but gives this error. " The method setPreferredWidth(int) is undefined for the type JComboBox"Sayre
Looking in the API, the exact method is: setPreferredSize(Dimension preferredSize)Katlin
K
1

Use a different LayoutManager. Try FlowLayout.

Katlin answered 7/1, 2011 at 18:59 Comment(1)
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.Katlin
S
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);
Squinty answered 7/1, 2011 at 21:7 Comment(0)
B
1

You might want to use setSize() method.

combo.setSize(200, combo.getPreferredSize().height);
Bloodstain answered 28/1, 2013 at 23:34 Comment(0)
H
-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

Hildredhildreth answered 20/10, 2022 at 6:23 Comment(1)
ComboBox is a heavyweight AWT component. AWT components and Swing components don't always play well together.Gurgitation

© 2022 - 2025 — McMap. All rights reserved.