cathalmchale
asked on
GridBagLayout
Hi,
I just read the sun tutorial for this layout and I cannot make any sense of it!! Could someone provide me with code for the following, and describe whats going on.
1 row, 3 columns
the middle component is of fixed size (can i just use setMin / Max / Preferred size here)
- the left and right component grow equally to fill remaining space
Thanks,
Cathal.
I just read the sun tutorial for this layout and I cannot make any sense of it!! Could someone provide me with code for the following, and describe whats going on.
1 row, 3 columns
the middle component is of fixed size (can i just use setMin / Max / Preferred size here)
- the left and right component grow equally to fill remaining space
Thanks,
Cathal.
This might also be of help: http://javaalmanac.com/cgi-bin/search/find.pl?words=gridbaglayout
in order to have a fixed length, the only way is to have using null layout
panel.setLayout(null);
All the other layout will adjust the components based on the resize.
If you still like to use resizing the components except few, then you have to write your own LayoutManager for that.
panel.setLayout(null);
All the other layout will adjust the components based on the resize.
If you still like to use resizing the components except few, then you have to write your own LayoutManager for that.
ASKER
OK, so what would the code look like for the stated?
>>1 row, 3 columns
>>the middle component is of fixed size (or of less size???)
>>- the left and right component grow equally to fill remaining space
>>1 row, 3 columns
>>the middle component is of fixed size (or of less size???)
>>- the left and right component grow equally to fill remaining space
for eg.,
JLabel label1 = new JLabel("first column");
label1.setLocation(<give the coordinates here>); // (x, y) format
label1.setSize(<required size here>); // (x, y) format
panel.setLayout(null);
panel.add(label1);
// repeat the same for the other components.
but this is not a good way to program because, the layout concept itself goes off.
When you resize, no component will resize in this. You can still capture the window events
and resize the components that you like to do.
JLabel label1 = new JLabel("first column");
label1.setLocation(<give the coordinates here>); // (x, y) format
label1.setSize(<required size here>); // (x, y) format
panel.setLayout(null);
panel.add(label1);
// repeat the same for the other components.
but this is not a good way to program because, the layout concept itself goes off.
When you resize, no component will resize in this. You can still capture the window events
and resize the components that you like to do.
The components themselves remain fixed, all that resize is the layout of the graph.
ASKER
sorry mmuruganandam,
i meant how would i do it with a GridBagLayout??
just want the left and right components to fill remaining area (horizontallly) - the middle component can be fixed ??
i meant how would i do it with a GridBagLayout??
just want the left and right components to fill remaining area (horizontallly) - the middle component can be fixed ??
You will need to re-set the size of the components (if it can be set) in order to do that. But why do you need to resize the components?
You have to use the GridBagConstraints for that
There is one attribute called fill. set the value to 1.
There is one attribute called fill. set the value to 1.
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZON TAL;
This way, you can fill the horizontal portion. BOTH to fill both. VERTICAL to vertical fill
c.fill = GridBagConstraints.HORIZON
This way, you can fill the horizontal portion. BOTH to fill both. VERTICAL to vertical fill
GridBagConstraints does not resize the componets but it just fills up the remaining space. If you want the components to grow proportionally to the original position then you will have to re-set the size somehow.
When the components are placed first, then it will occupy the remaining portion HORIZONTALLY. When you resize the window, again this will happen.
ASKER
Ya thats what i thought. So what is the code look like to add these 3 components components to the one row - where components 1 and 3 are the components to fill and resize appropriately??
> in order to have a fixed length, the only way is to have using null layout
thats not correct.
in this case i'd suggest using a BoxLayout instead of a GBL
thats not correct.
in this case i'd suggest using a BoxLayout instead of a GBL
ASKER
Any further ideas on this??
I think GridBagLayout is the one for me (but dunno)!
I like BoxLayout so tried it first, but when I add a panel with BoxLayout to BorderLayout.TOP (so that it occupies the entire horizontal) then the 1st 2 components are a small size (I fixed size of 2nd, but not first) but the 3rd component expands massively to occupy the rest of the space.
I think I over-stressed the "fixed size" issue - its more that i want component 1 and 3 to expand equally to fill the space - component 2 should remain much smaller???
I think GridBagLayout is the one for me (but dunno)!
I like BoxLayout so tried it first, but when I add a panel with BoxLayout to BorderLayout.TOP (so that it occupies the entire horizontal) then the 1st 2 components are a small size (I fixed size of 2nd, but not first) but the 3rd component expands massively to occupy the rest of the space.
I think I over-stressed the "fixed size" issue - its more that i want component 1 and 3 to expand equally to fill the space - component 2 should remain much smaller???
ASKER
Solution:
private static JPanel createEditor()
{
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
// 3 components, middle component should remain fixed size, 1 and 3 should
// resize equally to fill the horrizontal
/** variable combo */
c.fill = GridBagConstraints.HORIZON TAL;
c.gridx = 0;
c.gridy = 0;
panel.add(varCombo, c);
varCombo.setEditable(true) ;
AutoCompletion.enable(varC ombo);
/** value field */
c.gridx = 2;
c.gridy = 0;
panel.add(valField, c);
valField.addActionListener (new ActionListener()...
/** operator combo */
c.weightx = 0.05;
restrictSize(opCombo, OP_SIZE);
c.fill = GridBagConstraints.NONE;
c.gridx = 1;
c.gridy = 0;
panel.add(opCombo, c);
return panel;
}
private static JPanel createEditor()
{
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1;
// 3 components, middle component should remain fixed size, 1 and 3 should
// resize equally to fill the horrizontal
/** variable combo */
c.fill = GridBagConstraints.HORIZON
c.gridx = 0;
c.gridy = 0;
panel.add(varCombo, c);
varCombo.setEditable(true)
AutoCompletion.enable(varC
/** value field */
c.gridx = 2;
c.gridy = 0;
panel.add(valField, c);
valField.addActionListener
/** operator combo */
c.weightx = 0.05;
restrictSize(opCombo, OP_SIZE);
c.fill = GridBagConstraints.NONE;
c.gridx = 1;
c.gridy = 0;
panel.add(opCombo, c);
return panel;
}
Nice one :)
Please if you feel you have been helped accept an answer/split points otherwise ask a question for zero points to community support (https://www.experts-exchange.com/Community_Support/) to PAQ & refund the points back to you.
Please if you feel you have been helped accept an answer/split points otherwise ask a question for zero points to community support (https://www.experts-exchange.com/Community_Support/) to PAQ & refund the points back to you.
It's fine by me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can.
Is there any specific question you have about the layout?