help on gridBagLayout

M

Mr. X.

Helo,
I think that gridBagLayout is the most complicated of the standard layouts.

I try to understand the following (see my thoughts after the asterisks ...)
:

// (The code is a class that implements the JPanel)
protected void makebutton(String name,

GridBagLayout gridbag,

GridBagConstraints c) {

JButton button = new JButton(name);

gridbag.setConstraints(button, c);

add(button);

button.addActionListener(this);

}


public void initX() {

GridBagLayout gridbag = new GridBagLayout();

GridBagConstraints c = new GridBagConstraints();


setFont(new Font("SansSerif", Font.PLAIN, 14));

setLayout(gridbag);


setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

c.anchor = GridBagConstraints.LINE_END;

c.weightx = 1;

c.weighty = 1;

c.fill = GridBagConstraints.VERTICAL;

c.anchor = GridBagConstraints.EAST;

makebutton("ButtonX", gridbag, c);


c.weightx = 1;

c.weighty = 1;

c.gridwidth = GridBagConstraints.REMAINDER;

c.fill = GridBagConstraints.BOTH;

c.anchor = GridBagConstraints.EAST;

makebutton("ButtonY", gridbag, c);

// *********************************

// *********************************

// *********************************

// ...

All I want to do is that : ButtonX will be at the east side of the panel,
and ButtonY will be at the rest of the panel.



What I get is that buttonX is OK, but buttonY is from left to a place where
the button seems as a square (what ever I try to do ...)

Since I am new to gridbaglayout , I need help of the above code, please.

I would like a good tutorial (that explains with some examples, every method
& field of GridBagConstraint), please.

What's wrong with my code ?

Thanks :)
 
M

Mr. X.

....
I want that both buttonX and buttonY will fill the whole JPanel (the
container that they are on).

Thanks :)
 
M

Mr. X.

Thanks ...

This doesn't much help, since I have problem on the above code, because
ButtonY doesn't fill the rest of the area of it's container.

(If I am not wrong, HTML table has width = *, which does the task, which is
simplier).
 
R

RedGrittyBrick

Mr. X. said:
Helo,
I think that gridBagLayout is the most complicated of the standard layouts.

True. That's one reason why I prefer ....
- other Layout Managers where appropriate
- nested layouts
- MigLayout
I JGoodies FormLayout is also better in many respects
[code snipped]

All I want to do is that : ButtonX will be at the east side of the panel,
and ButtonY will be at the rest of the panel.



What I get is that buttonX is OK, but buttonY is from left to a place where
the button seems as a square (what ever I try to do ...)

Since I am new to gridbaglayout , I need help of the above code, please.

Try playing with Knute Johnson's teaching applet
http://rabbitbrush.frazmtn.com/gridbagtester.html

I would like a good tutorial (that explains with some examples, every method
& field of GridBagConstraint), please.

http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html
 
R

Roedy Green

All I want to do is that : ButtonX will be at the east side of the panel,
and ButtonY will be at the rest of the panel.

GridBagConstraints.BOTH
weightx 100%
ipadx
are your tools to make a cell wider
 
M

Mr. X.

Great !
That's work.

A thing I don't understand is,
why when for both "ButtonX" and "ButtonY", when the weightx = 1,
then I have the problem I have described (the buttons don't both fill their
container),
and when ButtonX's weight=0, and ButtonY=1, everything works fine.

Thanks :)
 
K

Knute Johnson

Mr. X. said:
Great !
That's work.

A thing I don't understand is,
why when for both "ButtonX" and "ButtonY", when the weightx = 1,
then I have the problem I have described (the buttons don't both fill their
container),
and when ButtonX's weight=0, and ButtonY=1, everything works fine.

Thanks :)

Is the container bigger than the buttons? GBL will only take more space
if there is more space to take.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top