1

I have a frame which contains a vertical toolbar with a combobox and some buttons. The combobox takes up the maximum height it can in the toolbar. Why? And how to solve this? Is there a way to fix the size of the combobox?

The code:

import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToolBar;
import javax.swing.border.BevelBorder;

public class Clipping extends JPanel {

    public Clipping()
    {
        setLayout(new BorderLayout());
        JToolBar toolbar = new JToolBar(JToolBar.VERTICAL);
        CreateToolBarButtons(toolbar);
        toolbar.setFloatable(false);
        toolbar.setBorder(new BevelBorder(BevelBorder.RAISED));
        add(toolbar, BorderLayout.WEST);
    }

    private static void CreateToolBarButtons(JToolBar toolbar)
    {
        String[] cboList = {"Line", "Polygon"};
        JComboBox cboDraw = new JComboBox(cboList);
        JButton btnClip = new JButton("Set clip area");
        JButton btnClear = new JButton("Clear");
        toolbar.add(cboDraw);
        toolbar.addSeparator();
        toolbar.add(btnClip);
        toolbar.addSeparator();
        toolbar.add(btnClear);
    }

    public static void main(String[] args) 
    {
        CreateFrame();
    }

    private static void CreateFrame()
    {
        JFrame frame = new JFrame("Clipping");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Clipping());
        frame.setSize(500,500);
        frame.setVisible(true);
    }
}
Saiyan
  • 197
  • 7
  • 22

1 Answers1

0

JToolbar uses a BoxLayout and JComboBox has an issue with it. See this question for a solution. Rather than creating a subclass, try to just setMaximumSize on the combo box with the height that you like.

Community
  • 1
  • 1
Andrei Vajna II
  • 4,642
  • 5
  • 35
  • 38