im trying to build a GUI for my Java application. I dynamically add JPanels, which are a "row" in my GUI. Under the List of the Panels there is a button to add a new Panel. As the List grows, it reaches the end of the parent Container. Then I want to use scrollbars, so that the user is able to reach every panel in the List.
So far I experimented with JScrollPanes and Layouts but I have no Idea how to get it working.
Can you give me some advice?
this.setLayout(new BorderLayout());
JPanel listContainer = new JPanel();
listContainer.setLayout(BoxLayout(listContainer, BoxLayout.Y_AXIS);
this.add(new JScrollPane(listContainer), BorderLayout.CENTER);
this.add(new JButton(), BorderLayout.PAGE_END);
//then i add panels to the listContainer. this wont work, when the space is complettly used there is no scrollbar.
You probably forgot to call
revalidate()
on yourlistContainer
(and you made a bunch of syntax errors).BoxLayout
does not do a great job in my opinion, but that is not the matter here.Here is a small demo code which seems to work quite well:
It's difficult to assertain from your code snippet exactly where you might be having problems.
You really need to provide a SSCCE which would allow us to diagnose the problems you are having.
Personally, and this will depend on you requirements, a better layout manager might be
VerticalLayout
from SwingLabs SwingX libraries.