The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JComboBox  [12 examples]

e764. 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);

 Related Examples
e756. Creating a JComboBox Component
e757. Getting and Setting the Selected Item in a JComboBox Component
e758. Getting the Items in a JComboBox Component
e759. Adding and Removing an Item in a JComboBox Component
e760. Selecting an Item in a JComboBox Component with Multiple Keystrokes
e761. Determining If the Menu of a JComboBox Component Is Visible
e762. Displaying the Menu in a JComboBox Component Using a Keystroke
e763. Displaying the Menu in a JComboBox Component Using a Keystroke If the Selected Item Is Not Unique
e765. Listening for Changes to the Selected Item in a JComboBox Component
e766. Listening for Action Events from a JComboBox Component
e767. Determining When the Menu of a JComboBox Component Is Displayed

See also: Actions    JButton    JCheckBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.