Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a JPanel expand to max width in another JPanel

I feel I need to rephrase the question a bit.

Updated question below.

I have a JPanel that contains:

myjpanel.setLayout(new BoxLayout(selectors, BoxLayout.PAGE_AXIS));

It contains the following three panels:

JPanel with fixed size 'x' and 'y'

JPanel with no fixed size

JPanel with no fixed size and small height

The second JPanel contains a JTable so it expands to fill the full height and pushes the bottom panel all the way down, as expected.

Like this:

t
t
m
m
m
m
m
b

t = top panel, m = middle panel, b = bottom panel.

That works. But the bottom panel does not feel like filling the entire width of the parent which is a problem.

ttt
mmm
 b 

I would like either this, where the panel fills the entire width:

ttt
mmm
bbb

or this, where the bottom panel is left centered:

ttt
mmm
b

Old question below:

I have a JPanel that contains:

.setLayout(new BoxLayout(selectors, BoxLayout.PAGE_AXIS));

Within it, there are three more JPanels. The first two are of fixed size and the middle one isn't.

I want my bottom panel to take only the height it needs, but uses all the available width of the outer JPanel.

I have tried using glue but to no avail, and I would rather not set preferred and min/max sizes. Is there a way to tell the component to "Fill the entire parents width" using just the layout manager and framework. I would rather not start to do hacks like setting sizes and overriding methods.

Note: I can't put any glue or filler in the inner panel, only the outer panel and its layout manager can be modified.

Attempt 1:

Using myPanel.setLayout(new GridLayout(3, 1)); did not produce the expected results. It made a grid like this:

XX
 X

But I expected:

X
X
X

Attempt 2:

Using myPanel.setLayout(new GridLayout(0,1)); did not produce the expected results. It made a grid like this:

x
x
x

But all panels were of the same size, ignoring the restraints.

like image 973
Martin Nielsen Avatar asked Jan 10 '13 13:01

Martin Nielsen


People also ask

What is extend JPanel Java?

extends in Java means is-a. So your class is a JPanel; it's inheritance; a fundamental part of OOP. Since your class is a JPanel, if you want to set it's background color somewhere inside the class you'd just do setBackground(yourColor) from outside would be ui.

How do you increase the size of a panel in Java?

It's this one. buttonPanel = new JPanel(); buttonPanel. setSize(new Dimension(30, 100));

How do I change the layout of a JPanel in Java?

The most common JPanel constructor has no parameters. This creates a panel with the default layout (FlowLayout). Call setLayout() to change the layout. JPanel p = new JPanel(); p.

How to set the size of a jpanel frame?

you need to set the size of the JPanel, that is going to be the frame's content via the method setPreferredSize () instead of using setSize (). Then the frame will wrap around the content when invoking pack (). It doesn't make sense to me, but it works. B.t.w.:

How to create a jpanel in Java?

In this tutorial, you will learn how you can create a JPanel in Java. ? button = new JButton ("Click Me!"); This is the same example in our JButton tutorial with little modifications. In this example, we have created our frame, button, and our panel. The button was placed inside the panel, and the panel was placed inside the frame.

Is it possible to specify jpanel's size on multiple levels?

B.t.w.: If you have more than one level/layer of JPanels, then it seems to be enough to specify the JPanel's size on only one level. Last edited by VS.; 11-08-2012 at 01:59 PM.

Does the bottom panel fill the entire width of the parent?

But the bottom panel does not feel like filling the entire width of the parent which is a problem. Within it, there are three more JPanels. The first two are of fixed size and the middle one isn't.


1 Answers

The easiest way would be to use another layout manager such as GridLayout that automatically sizes components to fill the parent container.

myPanel.setLayout(new GridLayout(0, 1));
like image 158
Reimeus Avatar answered Oct 08 '22 17:10

Reimeus