Karen
Karen

Reputation: 275

Using different layouts in GUIs

This is what my program should look like and I'm a bit confused on where I should use different layouts.

I have a Window class which calls the Panel class and the Panel class calls the InputPanel and DisplayPanel classes. My InputPanel class calls my DetailsPanel, CrimePanel and ButtonPanel classes so they make up what is seen under the Input tab. I was told to use a BorderLayout for the whole window, and that the DetailsPanel (left panel) and CrimePanel should be GridLayout.

Does this mean I should:

  1. Put the BorderLayout code in Panel and the GridLayout code in CrimePanel and DetailsPanel or
  2. Put the BorderLayout code in Window and the GridLayout code in Panel?

alt text http://img137.imageshack.us/img137/6422/93381955.jpg

Upvotes: 1

Views: 1083

Answers (3)

Joey
Joey

Reputation: 354476

Okay, your description is a little bit confusing (or I'm still too tired today or didn't have enough caffeine yet). Your notion of "calling" panel classes from others is also a little weird.

But as far as I can see it, your first option is the correct one.

In general you just nest the objects at runtime, so it might look a little like the following:

InputPanel (has BorderLayout)
+--DetailsPanel (put in BorderLayout.WEST; has GridLayout)
|  +--nameLabel
|  +--nameTextField
|  +--...
+--CrimePanel (put in BorderLayout.NORTH; has GridLayout)
|  +--murderRadioButton
|  +--arsonRadioButton
|  +--...
+--ButtonPanel (put in BorderLayout.CENTER; has GridLayout)
   +--button

You usually do this in the constructor of the appropriate class:

public class InputPanel {
    public InputPanel() {
        this.setLayout(new BorderLayout());
        this.add(new DetailsPanel(), BorderLayout.WEST);
        this.add(new CrimePanel(), BorderLayout.NORTH);
        this.add(new ButtonPanel(), BorderLayout.CENTER);
    }
}

public class DetailsPanel {

    JLabel nameLabel;
    JTextField nameField;
    // ...

    public DetailsPanel() {
        this.setLayout(new GridLayout(5, 1));

        nameLabel = new JLabel("Name");
        nameField = new JTextField();
        // ...

        this.add(nameLabel);
        this.add(nameField);
        // ...
    }
}

...

However, I see a small problem here: Since GridLayout doesn't allow components to span multiple columns you may need to nest other panels in the DetailsPanel on the left as well. You can get away with a single GridBagLayout which has the needed capabilities, or you nest other panels there:

DetailsPanel (has BorderLayout)
+--panel1 (has GridLayout with 2 rows, 1 column; put in BorderLayout.NORTH)
|  +--nameLabel
|  +--nameField
+--panel2 (has GridLayout with 3 rows, 2 columns; put in BorderLayout.CENTER)
   +--dayField
   +--dayLabel
   +--monthField
   +--...

Upvotes: 5

Michael Borgwardt
Michael Borgwardt

Reputation: 346270

First of all you will need to put a JTabbedPane into the Window to contain your two tabs (input and display), each consisting of a JPanel.

The input panel could be subdivided using a BorderLayout as Joannes describes; another alternative is the GroupLayout introduced in Java 6, which is very powerful, but hard to wrap your mind around. It could be used to layout the entire tab in one panel.

Upvotes: 0

Matthew Murdoch
Matthew Murdoch

Reputation: 31453

From your description, option 1 should work. Give the Input JPanel a BorderLayout, with the Details, Crime and Button JPanels (at the west, north and south, respectively) having GridLayouts.

Upvotes: 0

Related Questions