?
Solved

GridBagLayout

Posted on 2004-08-17
20
Medium Priority
?
987 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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