Solved

GridBagLayout

Posted on 2004-08-17
20
947 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now