Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setup GUI using grid bag layout

Posted on 1997-06-22
5
Medium Priority
?
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
This video teaches viewers about errors in exception handling.
Suggested Courses

730 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