e775. 设置JList组件项的维数

时间:2023-03-10 04:28:37
e775. 设置JList组件项的维数

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