How to set fixed width of JComboboxs popup menu that uses GridBagLayout and fill=HORIZONTAL ?
One of the things I've tried is just to override the getSize() method, but the dose does not work.
public class ComboBoxSize extends JFrame { public static void main(String args[]) { // THE COMBOBOX String labels[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; JComboBox<String> comboBox = new JComboBox<String>(labels) { public Dimension getSize() { Dimension d = getPreferredSize(); d.width = 50; return d; } }; comboBox.setMaximumRowCount(comboBox.getModel().getSize()); // ADD COMBOBOX TO PANEL JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.weightx = 1; c.fill = GridBagConstraints.HORIZONTAL; panel.add(comboBox, c); // ADD PANEL TO FRAME JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(panel); frame.setSize(300, 200); frame.setVisible(true); } }
source share