Solved

GridBagLayout

Posted on 2004-08-17
20
970 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:cathalmchale
  • 7
  • 5
  • 5
  • +2
20 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 11819377
> (can i just use setMin / Max / Preferred size here)

You can.

Is there any specific question you have about the layout?
0
 
LVL 35

Expert Comment

by:girionis
ID: 11819384
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 11819389
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.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:cathalmchale
ID: 11819417
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
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 11819448
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.

0
 
LVL 35

Expert Comment

by:girionis
ID: 11819486
The components themselves remain fixed, all that resize is the layout of the graph.
0
 

Author Comment

by:cathalmchale
ID: 11819512
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 ??
0
 
LVL 35

Expert Comment

by:girionis
ID: 11819534
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?
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 11819552
You have to use the GridBagConstraints for that

There is one attribute called fill.  set the value to 1.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 11819584
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;

This way, you can fill the horizontal portion.  BOTH to fill both.  VERTICAL to vertical fill
0
 
LVL 35

Expert Comment

by:girionis
ID: 11819613
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.
0
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 11819638
When the components are placed first, then it will occupy the remaining portion HORIZONTALLY.  When you resize the window, again this will happen.
0
 

Author Comment

by:cathalmchale
ID: 11820194
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??
0
 
LVL 92

Expert Comment

by:objects
ID: 11826486
> 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
0
 

Author Comment

by:cathalmchale
ID: 11830801
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???
0
 

Author Comment

by:cathalmchale
ID: 11881734
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.HORIZONTAL;
    c.gridx = 0;
    c.gridy = 0;
    panel.add(varCombo, c);
    varCombo.setEditable(true);
    AutoCompletion.enable(varCombo);
    /** 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;
  }
0
 
LVL 35

Expert Comment

by:girionis
ID: 11882948
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 (http://www.experts-exchange.com/Community_Support/) to PAQ & refund the points back to you.
0
 
LVL 35

Expert Comment

by:girionis
ID: 11883780
It's fine by me.
0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 11920674
Closed, 50 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 56
java stored proc example 9 44
How do I remove an object from a 3 40
Setup GlassFish 4 21
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question