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 |