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.
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.
It's this one. buttonPanel = new JPanel(); buttonPanel. setSize(new Dimension(30, 100));
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.
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.:
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.
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.
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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With