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

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.