Solved

Layout question

Posted on 2004-04-08
12
207 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 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

688 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