Make a panel resize dynamically in Swing

I'm creating an interface composed out of a box layout which contains panels inside every space.

In this specific case I've got a "cascade" of panels, the first is supposed to be a panel with a FlowLayout as layout manager, underneath it there's a GridLayout and under it there's supposed to be another label.

The thing is that I'd need the first panel to dynamically resize as the window get resized itself.

Here's the problem: I need the first panel to have a specific size in relation to the absolute size.. the thing is that I can't set my preferred size.. in the class I do the following but the panel stays the exact same size as before..

this.setPreferredSize(new Dimension(windowWidth, 50))

I'll send the code as soon as I get home, for now the situation is the one written above.

Upvotes: 0

Views: 1572

Answers (1)

camickr
camickr

Reputation: 324207

I need the first panel to have a specific size in relation to the absolute size

Your main panel should be a BorderLayout (which is the default layout of a JFrame), not a BoxLayout.

Then you add the panel that needs to be a specific size to the BorderLayout.NORTH of the frame.

Then your second panel can use a BoxLayout and add this panel to BorderLayout.CENTER of the frame. Now this panel will get all the extra space as the frame is resize.

Upvotes: 1

Related Questions