Setting the Number of Visible Items in the Menu of a JComboBox Component

By default, the menu of a combobox only shows eight items. If there are more items in the menu, a scrollbar is automatically created. To change the default of eight, use JComboBox.setMaximumRowCount().
// Create a read-only combobox with lots of items String[] items = new String[50]; for (int i=0; i<items.length; i++) { items[i] = "" + Math.random(); } JComboBox cb = new JComboBox(items); // Retrieve the current max visible rows int maxVisibleRows = cb.getMaximumRowCount(); // Change the current max visible rows maxVisibleRows = 20; cb.setMaximumRowCount(maxVisibleRows);

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.