How to set scrollbar in JList and how to set the size of JList. I have a jList and in JList I want to set fix height. If I have data more then that height I want to show a scrollbar.
Asked
Active
Viewed 1.2k times
2
-
Wrap it in JscrollPane – Ben Rhouma Zied May 21 '15 at 10:14
-
Beware, using `setPreferredSize` on a `JList` will prevent the `JList` from changing size in relationship to it's model. You should be using `setVisibleRowCount` and/or `setPrototypeCellValue` in order to provide hints about it's size and viewport preferred sizes. – MadProgrammer Apr 05 '16 at 21:08
2 Answers
3
To set a JLit in scrollbar
JList<Object> list = new JList<Object>();
JScrollPane sp = new JScrollPane(list);
To size the JList
list.setPreferredSize(new Dimension(200, 200));

Normal design
- 148
- 5
-
[Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi). The problem with this is it's going to restrict both the width and height of the `JList` and prevent it from changing size in relationship to it's model – MadProgrammer Apr 05 '16 at 21:07
2
how to set scrollbar in JList
Wrap the JList
to a JScrollPane
JList list = ...;
JScrollPane sp = new JScrollPane(list);
// Add the scrollpane to the container
See How to use scroll panes for more details
and how to set size of JList.
You can use JList#setVisibleRowCount
to affect the the number of visible rows the JList
would like to show when wrapped in a JList
.
You can also use JList#setPrototypeCellValue
which is the "default" value the JList
will use to calculate the size of individual cells, this is intended as a performance enhancement, but will effect the width of the JList

MadProgrammer
- 343,457
- 22
- 230
- 366