I have the following code:
JList<Song> list = new JList<Song>(this.lib);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(3);
list.setSize(1, 1);
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
list.setSize(10, 10);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
add(list);
I am not sure what is wrong here. When I look at the JFrame, I see the list in a single box with all 9 items that I have in my list. I tried messing with the size to see if I could get that to work but it isn't. The size doesn't seem to change no matter what I set it to.
My goal is to have a vertical and horizontal scroll-bar when necessary and a JList that is of some fixed size (fixed compared to the frame size would be best if possible).
You're adding the list
to the container, not the listScroller
.
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
list.setSize(10, 10);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
add(list);
You should try using something like...
JScrollPane listScroller = new JScrollPane();
listScroller.setViewportView(list);
setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
//add(list);
add(listScroller);
I think you will also find list.setSize(10, 10);
is pointless, as the JScrollPane
(actually the viewport, but lets keep it simple) has it's own layout manager.
You should take a look at Jlist#setVisibleRowCount
if you want to affect the number of visible rows a JList
will show before it needs to be scrolled.
The actual width and height of each row is determined by the ListCellRenderer
, but you can use JList#setPrototypeCellValue
to affect how these might be calculated.