Solved

Setup GUI using grid bag layout

Posted on 1997-06-22
5
225 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:pehlivanian
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:jpk041897
ID: 1221901
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
 
LVL 2

Expert Comment

by:webster030697
ID: 1221902
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
 

Author Comment

by:pehlivanian
ID: 1221903
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
 
LVL 2

Accepted Solution

by:
webster030697 earned 100 total points
ID: 1221904
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
 

Author Comment

by:pehlivanian
ID: 1221905
Bingo...nice work.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

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

9 Experts available now in Live!

Get 1:1 Help Now