Solved

Setup GUI using grid bag layout

Posted on 1997-06-22
5
227 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 80
Free Alternative to JIRA 4 91
Java. Convert method from recursion based to iteration based ( loop based ) 6 79
replace a word with other 1 40
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

864 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

19 Experts available now in Live!

Get 1:1 Help Now