java BoxLayout panel's alignment
Asked Answered
B

3

15

I have browsed around and haven't found a solution that specifically tailors to my situation. I have a panel that I display in a dialog box:

//create dialog panel
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.add(headerPanel);
panel.add(type1Panel);
panel.add(type2Panel);
panel.add(type3Panel);
panel.add(type4Panel);
panel.add(type5Panel);
panel.add(type6Panel);

int result = JOptionPane.showConfirmDialog(null, panel, "Please enter values.", JOptionPane.OK_CANCEL_OPTION);

The size of the last two panels, type5 & type6, are of equal size so they look fine. However, the header and first 4 panels are of different sizes and I would like them all to be left aligned. As of yet I haven't found a good solution as how to fix this.

Question is, how can I left align the first 5 panels, but not last 2? If not how can I left align them all? The setalignmentx() isn't available for panels. I've tried using GridLayout, but then the width of the gui's main window is rather large and doesn't fit nicely onto the screen, hence the BoxLayout along Y axis.Thanks for any help or suggestions.

Bertiebertila answered 9/2, 2012 at 13:57 Comment(4)
working with this right now: headerPanel.setAlignmentX(Component.LEFT_ALIGNMENT); But appears to be willy nilly. I've seen people mention to avoid doing this.Bertiebertila
The setalignmentx() isn't available for panels. - setAlignmentX() is available for me. Could you clarify?Cubital
Well I figured out the problem. If you do panel.add(headerPanel); then do the headerPanel.setAlignmentX(Component.LEFT_ALIGNMENT); it won't align correctly. However, doing the alignment first, then adding the headerPanel to the dialog's panel it works just fine. Go figure.Bertiebertila
I was using headerPanel.setAlighmentX(Component.LEFT_ALIGNMENT);. the setAlignmentX was looking for a type float? And wasn't working.Bertiebertila
H
30

Here is an example that will left align all the JPanels added to the panel used as a container.

   JPanel a = new JPanel();
   JPanel b = new JPanel();
   JPanel c = new JPanel();

   a.setBackground( Color.RED );
   b.setBackground( Color.GREEN  );
   c.setBackground( Color.BLUE );

   a.setMaximumSize( new Dimension(  10, 10) );
   b.setMaximumSize( new Dimension(  50, 10) );

   a.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
   b.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0
   c.setAlignmentX( Component.LEFT_ALIGNMENT );//0.0

   JPanel panel = new JPanel();
   panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
   panel.add(a);
   panel.add(b);
   panel.add(c); 

   int result = JOptionPane.showConfirmDialog(null, panel, "Please enter values.", JOptionPane.OK_CANCEL_OPTION);
Homogony answered 9/2, 2012 at 14:8 Comment(3)
The methods setAlignmentX and setAlignmentY take a float, just to be clear. JComponent has the following alignment constants inherited from java.awt.Component: BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT. Using these makes the code more readable, at least to me.Crowning
@mre Why? For this example it serves only to add complexity. The code is simple and readable enough. Simplicity over regularity for examples such as this.Pelecypod
because having and using named & systematic constants is always better than supplying magic numbers?Regression
R
18

Create a horizontal javax.swing.Box object to contain each typenPanel object. Using horizontal struts and glue you can do whatever you want:

Box  b1 = Box.createHorizontalBox();
b1.add( type1Panel );
b1.add( Box.createHorizontalGlue() );
panel.add( b1 );

For simplicity, write a helper method to do this for you:

private Component leftJustify( JPanel panel )  {
    Box  b = Box.createHorizontalBox();
    b.add( panel );
    b.add( Box.createHorizontalGlue() );
    // (Note that you could throw a lot more components
    // and struts and glue in here.)
    return b;
}

Then:

panel.add( leftJustify( headerPanel ) );
panel.add( leftJustify( type1Panel ) );
panel.add( leftJustify( type2Panel ) );

etc.... You can get fancier with each line, adding components, glue, and struts. I've had great luck deeply nesting vertical and horizontal boxes, and writing helper methods when I want to do the same layout in a box more than once. There's no limits to what you can do, mixing components, struts, and glue as necessary.

I'm sure there's a better way to do all this, but I haven't found it yet. And the dynamic resizing lets a user with short bits of text use a small window and a user with lots of text resize it so it all fits.

Reinert answered 9/2, 2012 at 15:0 Comment(0)
C
5

You should use setAlignmentX on the panels because it is available for JPanel. The methods setAlignmentX and setAlignmentY are found in JComponent, which JPanel extends. It works...I've got code that uses those methods to align JPanels in a BoxLayout.

Ok, fine, edit your question while I'm answering it :)

Instead of using a JPanel try using a Box. I've found the Box class to be very useful as a container. From the API:

A lightweight container that uses a BoxLayout object as its layout manager. Box provides several class methods that are useful for containers using BoxLayout -- even non-Box containers.

If you haven't seen it yet, the tutorial How to Use BoxLayout is very helpful.

Crowning answered 9/2, 2012 at 14:8 Comment(1)
Thanks for this. setAlignmentX is useful for all JComponent instances.Harmonium

© 2022 - 2024 — McMap. All rights reserved.