Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Setup GUI using grid bag layout

Posted on 1997-06-22
5
Medium Priority
?
257 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 400 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 to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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

886 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