Solved

Layout question

Posted on 2004-04-08
12
202 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
Comment Utility
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
Comment Utility
Use a BoxLayout
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
>  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
 
LVL 92

Expert Comment

by:objects
Comment Utility
>  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
Comment Utility
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:mayankeagle
Comment Utility
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
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
Comment Utility
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
Comment Utility
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:mayankeagle
Comment Utility
Glad to know you made it ;-)
0
 

Author Comment

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

Expert Comment

by:objects
Comment Utility
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now