?
Solved

Layout question

Posted on 2004-04-08
12
Medium Priority
?
208 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
[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
  • 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 100 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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