You should use setPreferredSize
instead of setSize
. The BorderLayout
will notice this and resize the panel on the next validate()
.
Java JPanel expand one panel in BorderLayout
-
04-06-2022 - |
Question
I have two simple JPanels, one positioned on top and the other on the bottom.
Now im trying to resize the top panel to take up more space (and therefore push the bottom panel down)
Ive added a button where i listen on call like:
button.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent e) {
panelTop.setSize(10, 200);
}
});
This is how i add my panels:
add(panelTop, BorderLayout.NORTH);
add(panelBottom, BorderLayout.SOUTH);
Any ideas if this is possible?
My tests are not working at all
Solution 2
OTHER TIPS
What you are trying to do can be easily solved with a JSplitPane
This will take two components and put a divider between the two that you can use to re-size them.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow