No, don't set the size of the JList
which is already added to JScrollpane
. Because JList
has an implemented function getPreferredScrollableViewportSize()
which computes preferable viewport
size by computing cell width and cell height it contains. Let the JScrollPane
to handle that. If require, you try adding JScrollPane's
dimension. However, to control the item display number and orientation, you can use:
setLayoutOrientation
which lets the list display its data in multiple columns. The value:JList.HORIZONTAL_WRAP
: specifies that the list should display its items from left to right before wrapping to a new row.JList.VERTICAL_WRAP
: which specifies that the data be displayed from top to bottom (as usual) before wrapping to a new column.
setVisibleRowCount(-1)
makes the list display the maximum number of items possible in the available space onscreen. Another common use ofsetVisibleRowCount
is to specify to the lists's scroll pane how many rows the list prefers to display.
Check out: How to Use List for Demo and examples.