The Java Developers Almanac 1.4


Order this book from Amazon.

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

e775. Setting the Dimensions of an Item in a JList Component

By default, the width of the list is determined by the longest item and the height is determined by the number of visible lines multiplied by the tallest item. This example demonstrates how to override these values.
    // Create a list
    String[] items = {"A", "B", "C", "D"};
    JList list = new JList(items);
    
    // Set the item width
    int cellWidth = 200;
    list.setFixedCellWidth(cellWidth);
    
    // Set the item height
    int cellHeight = 18;
    list.setFixedCellHeight(cellHeight);
It is also possible to set the item dimensions using a sample value:
    String protoCellValue = "My Sample Item Value";
    list.setPrototypeCellValue(protoCellValue);

 Related Examples
e774. Creating a JList Component
e776. Setting a Tool Tip for an Item in a JList Component
e777. Getting the Items in a JList Component
e778. Adding and Removing an Item in a JList Component
e779. Getting the Selected Items in a JList Component
e780. Setting the Selected Items in a JList Component
e781. Setting the Selection Mode of a JList Component
e782. Arranging Items in a JList Component
e783. Detecting Double and Triple Clicks on an Item in a JList Component
e784. Listening for Changes to the Selection in a JList Component
e785. Listening for Changes to the Items in a JList Component

See also: Actions    JButton    JCheckBox    JComboBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    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.