How to set size of JList with Scrollbar in swing?

Avanish Kumar picture Avanish Kumar · May 21, 2015 · Viewed 8.9k times · Source

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.

Answer

Normal design picture Normal design · May 21, 2015

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