Solved

Layout question

Posted on 2004-04-08
12
206 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
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
type mismatch (Object[] to double[] 4 41
Java array sort 10 35
Pass multiple values or string arrays in java as a parameter 3 50
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

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