Solved

Layout question

Posted on 2004-04-08
12
204 Views
Last Modified: 2010-03-31
I have a panel that will have other panels dynamically added to it.  What I want to do is to add all of the panels in a vertical line, one on top of the other, but keeping their respective width and height (i.e. - not resizing to fit the parent panel).  So, the end result would be:

------------------
|     panel       |
-------------------
|     panel       |
-------------------
|     panel       |
------------------
|                    |
|                    |
|                    |
|                    |
|                    |
------------------

So, below that third panel is just empty space.  Hopefully this picture comes out :)

Thanks!
0
Comment
Question by:ctjoumas
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10786433
You can use something like this:


import javax.swing.*;
import java.awt.*;

class PanelLayout extends JPanel {

  public PanelLayout() {
    super(new BorderLayout());
    currentPanel = this;
  }

  private JPanel currentPanel;

  public void addPanel(JPanel newPanel) {
    JPanel flowPanel = new JPanel(new FlowLayout());  // to keep the original width
    flowPanel.add(newPanel);
    currentPanel.add(flowPanel, BorderLayout.NORTH);
    JPanel borderPanel = new JPanel(new BorderLayout());  // to use as center panel
    currentPanel.add(borderPanel, BorderLayout.CENTER);
    currentPanel = borderPanel;
  }

  public static void main(String[] args) {
    JPanel panel1 = new JPanel();
    JPanel panel2 = new JPanel();
    JPanel panel3 = new JPanel();
    JPanel panel4 = new JPanel();

    panel1.setBackground(Color.BLUE);
    panel2.setBackground(Color.RED);
    panel3.setBackground(Color.ORANGE);
    panel4.setBackground(Color.GREEN);

    panel1.add(new JLabel("<html>hello world 1</html>"));
    panel2.add(new JLabel("<html>hello world 2<br>hello world 2</html>"));
    panel3.add(new JLabel("<html>hello world 3<br>hello world 3<br>hello world 3</html>"));
    panel4.add(new JLabel("<html>hello world 4<br>hello world 4<br>hello world 4<br>hello world 4</html>"));

    PanelLayout test = new PanelLayout();
    test.addPanel(panel1);
    test.addPanel(panel2);
    test.addPanel(panel3);
    test.addPanel(panel4);

    JFrame myFrame = new JFrame();
    myFrame.getContentPane().setLayout(new BorderLayout());
    myFrame.getContentPane().add(test, BorderLayout.CENTER);
    myFrame.pack();
    myFrame.setVisible(true);
  }

}
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 10787220
Use a BoxLayout
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10787459
>  Use a BoxLayout
That makes it a little easier indeed.  But i guess you still need to use additional panels with FlowLayout if you want to keep the original width:


class PanelLayout extends JPanel {

  public PanelLayout() {
    super();
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
  }

  public void addPanel(JPanel newPanel) {
    JPanel flowPanel = new JPanel(new FlowLayout());
    flowPanel.add(newPanel);
    add(flowPanel);
  }

}
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 92

Expert Comment

by:objects
ID: 10787604
>  But i guess you still need to use additional panels with FlowLayout if you want to keep the original width

No BoxLayout would allow the panels to keep a certain width.
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10787928
I don't use BoxLayout much, but from my tests, it seems that each panel will get the same width, which is the width of the widest panel...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10789405
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10790285
BoxLayout is pretty nice indeed...  I'll try to use it more often.
From the "how to use BoxLayout" page, it seems that the width of the added components will be set to their MaximumWidth.
For JButtons, MaximumWidth = PreferredWidth, but not for JPanels, which makes the JPanel take the width of the widest component.
If you set the MaximumSize to the PreferredSize, you get different widths for your JPanels, but then the layout gets quite uggly.
There must be ways to tune it, but i find it easier to just put each JPanel in a new JPanel with FlowLayout.
0
 

Author Comment

by:ctjoumas
ID: 10792419
Actually, I couldn't get it to work with BoxLayout.  As the main panel was resized (in the vertical direction), the panels that were added expanded to fit the panel.  So, in the "drawing" I provided in my first post, those three panels will expand the entire panel.

I can get this to work with SpringLayout on the subpanels itself (which I don't really get...but it works).  However, if there is a way to get it to work with BoxLayout, I would probably prefer that.
0
 

Author Comment

by:ctjoumas
ID: 10792446
Ahh..actually, I got it.  When the panels are finished being added, I just add a Box.createVerticalGlue() to the main panel.  I gotta run, but ill be back to give the points out :)

Thanks!!!
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10793285
Glad to know you made it ;-)
0
 

Author Comment

by:ctjoumas
ID: 10793529
Thanks for all of your help - I guess I shoulda remembered about that verticalGlue guy...
0
 
LVL 92

Expert Comment

by:objects
ID: 10794695
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 65
split string containing \r\n in Java 46 43
session migration servlets 2 36
Running JavaFX on JDeveloper 12C 1 31
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
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 …
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…

786 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