Reputation: 4547
I so my problem is this:
I made a JPanel. Inside it I want to add a JList with a scroll-bar. So I use JScrollPane.
Here you can see a picture of the application. The big white recangle is the size of the JPanel. In the second picture you see what happens when I add the Scrollpane to the code.
Here is the code I use:
public class GUI extends JFrame{
DefaultListModel m = new DefaultListModel();
JList myList = new JList(m);
JScrollPane scrollPane = new JScrollPane(myList);
JPanel listPanel = new JPanel();
public GUI(){
//Stuff about Frame Size, title, and other boring things
scrollPane.setViewportView(myList);
listPanel.add(scrollPane);
this.add(listPanel);
}
}
I have used it before, and it worked. Well, I faced the same problem, but it went away. I have written this code the same way as I did the previous time. But this time it doesn't work.
thanks in advance guys.
Upvotes: 0
Views: 337
Reputation: 347194
With JList
you can simply use setVisibleRowCount
to adjust the size of the viewable area of the JScrollPane
The other problem is JPanel
uses a FlowLayout
by default, you may want to change the use something like BorderLayout
instead
Updated
As pointed out, if the list contains a large number of elements, you can use setPrototypeValue
to improve the efficiency
Upvotes: 1
Reputation: 591
By default, unless you use something like a grid bag layout...
Also, for developing UI's in Java, it is extremely helpful to install an eclipse plugin called WindowBuilder
Upvotes: 0
Reputation: 4547
Okay, so apparently I have to setPreferredSize. You can't just set the size of the panel, you need to set the size of the jscrolpane too!
Upvotes: 0