Setup GUI using grid bag layout

Within my applet, I would like to set up a user interface using the grid bag layout manager with the following setup:

6 cells,
top left corner is a label with the text "whatever" centered at top, taking up two column,
top right is a black canvas, taking up two columns,
the bottom row, from left to right, contains 4 buttons, labeled "one" to "four", say.
The 4 columns are given equal weighting, while the top row containing the label and the canvas should take up 80% of the height of the applet.

What is the easiest way to do this?

Thank you,
Ara Pehlivanian
arap@idt.net
pehlivanianAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jpk041897Commented:
I'm not quite sure if you would coun't this as an answer but...

The easiest way to set up gridbag layouts is to download a copy of lava from:

http://www.cs.nott.ac.uk/~dsp/lava/index.html

Its a JDK 1.1 app code generatorwith several bugs, but it can produce a layout as you have described in about 5 minutes. (4minutes and 55 seconds for you to draw the GUI and about 5 seconds to generate it :) ).

The code produced by lava is compatible with both JDK 1.02 and 1.1, and I have stopped designing GUI's by hand since I found it.

Note: It is still a buggy program, so save often.
0
webster030697Commented:
jpk's idea is a good one, but it is still buggy by his(assumption?) own admission.

I try to avoid GridbagLayout(). Try using panels to group things the way you want them. This code produces something like what you want, I think:

import java.awt.*;
import java.applet.*;

class LayoutTest extends Applet
{    
    // Label
       
    private Label MyLabel = new Label(" Whatever ");

    public void init()
    {  
          setLayout(new BorderLayout());
       
        // Main Panels for Layout
       
        Panel MidPanel = new Panel();
        MidPanel.setLayout(new GridLayout(1, 2));
        Panel LeftPanel = new Panel();
        LeftPanel.setLayout(new FlowLayout());
        Panel RightPanel = new Panel();
        RightPanel.setLayout(new FlowLayout());
        Panel ButtonPanel = new Panel();
        ButtonPanel.setLayout(new GridLayout(1, 4));
       
        // Add component Panels to Main Panels
       
        LeftPanel.add(MyLabel);
        ButtonPanel.add(new Button("One"));
        ButtonPanel.add(new Button("Two"));
        ButtonPanel.add(new Button("Three"));
        ButtonPanel.add(new Button("Four"));
       
        // Add main Panels to the Frame
       
        RightPanel.setBackground(Color.black);
       
        MidPanel.add(LeftPanel);
        MidPanel.add(RightPanel);
        add("Center", MidPanel);
        add("South", ButtonPanel);
       
    }
   
}

If you like this approach, maybe you could allow me to submit it as an answer.
0
pehlivanianAuthor Commented:
Ok, this works. Thank you. I implemented it as an application for the time being.
 
Regarding Lava; this finally forced me to update from 1.0 to 1.1...and the compiler now flouts half of my code as 'deprecated'...The program looks nice, though, and it may be my permanent solution.

Regarding the LayoutTest class: I hate to be picky, but is it easy to place those four buttons against a small gray backgound panel? If not, no problem. I have not used the GridLayout manager yet.


Thanks again,
Ara Pehlivanian
arap@idt.net
0
webster030697Commented:
Further to my last submission:

Yes you can give the buttons the appearance you are looking for. Instead of adding the buttons directly onto the ButtonPanel, create four Panels, add each button to one of the Panels, and then add the four Panels to ButtonPanel. ie

Panel p1 = new Panel();
p1.setLayout(new FlowLayout());
Panel p2 = new Panel();
p2.setLayout(new FlowLayout());
Panel p3 = new Panel();
p3.setLayout(new FlowLayout());
Panel p4 = new Panel();
p4.setLayout(new FlowLayout());

p1.add(new Button("One");
p2.add(new Button("Two");
p3.add(new Button("Three");
p4.add(new Button("Four");

ButtonPanel.add(p1);
ButtonPanel.add(p2);
ButtonPanel.add(p3);
ButtonPanel.add(p4);

I know it gets a bit messy, but that's the price you pay for platform independance.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pehlivanianAuthor Commented:
Bingo...nice work.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.