Arranging Items in a JList Component
By default, the items in a list are arranged vertically, in a single
column, as in:
It is possible to arrange the items left-to-right and top-to-bottom,
as in:
This example creates and configures a list that displays its items
left-to-right and top-to-bottom. Note that the number of columns
can change as the width of the list changes.
The items can also be arranged top-to-bottom and left-to-right, as in:
This example changes the layout orientation so that its items are
displayed top-to-bottom and left-to-right.
With some look and feels, a list is set to display a fixed number of
rows. In order to make the number of visible rows dependent on the
height of the list, the visibleRowCount property must be set to
0:
item1
item2
...
item1 item2
item3 item4
item5 ...
// Create a scrollable list
String[] items = {"A", "B", "C", "D"};
JList list = new JList(items);
JScrollPane scrollingList = new JScrollPane(list);
// The default layout orientation is JList.VERTICAL
int orient = list.getLayoutOrientation();
// Change the layout orientation to left-to-right, top-to-bottom
list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
item1 item4
item2 item5
item3 ...
// Change orientation to top-to-bottom, left-to-right layout
list.setLayoutOrientation(JList.VERTICAL_WRAP);
// Make the number of rows dynamic
list.setVisibleRowCount(0);
Post a comment