Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-08-2009, 12:13 PM
Member
 
Join Date: Feb 2009
Posts: 3
Rep Power: 0
PetalumaBoy is on a distinguished road
Default BoxLayout Behaviour
Dear Folks,

I do have an issue with the Java BoxLayout Manager.

if I use BoxLayout with Y-Axis all buttons within a JPanel
are as wide as their container, in other words have the same width.

if I use BoxLaout with X-Axis orientation all buttons within a JPanel
are not as wide as thier container, in other words have different hights.

Here a small example, switch the commented line:

import java.awt.Component;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.BoxLayout;

public class SameHight {

public static void main( String args[] ) {

JFrame frame = new JFrame("Box Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

Container contentPane = frame.getContentPane();

JPanel panel = new JPanel();
// panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ));
panel.setLayout( new BoxLayout( panel, BoxLayout.X_AXIS ));
contentPane.add(panel);

JButton b1 = new JButton("<html><body>Button 1</html></body>");
panel.add(b1);
JButton b2 = new JButton("<html><body>Button 2<br>Spring Water</html></body>");
panel.add(b2);
JButton b3 = new JButton("<html><body>Button 3</html></body>");
panel.add(b3);
JButton b4 = new JButton("<html><body>Button 4<br>Fountain Water</html></body>");
panel.add(b4);
JButton b5 = new JButton("<html><body>Button 5</html></body>");
panel.add(b5);
JButton b6 = new JButton("<html><body>Button 6<br>Moutain Spring Water</html></body>");
panel.add(b6);

frame.pack();
frame.setVisible(true);
}
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 06-08-2009, 01:06 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,447
Rep Power: 8
Fubarable is on a distinguished road
Default
One solution is to place each button into its own JPanel that uses BorderLayout, and place the jpanels into your boxlayout using container. For example:

Code:
import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class SameHeight {
  public static final String[] BUTTON_STRINGS = {
    "<html>Button 1</html>",
    "<html>Button 2<br>Spring Water</html>",
    "<html>Button 3</html>",
    "<html>Button 4<br>Fountain Water</html>",
    "<html>Button 5</html>",
    "<html>Button 6<br>Moutain Spring Water</html>"
  };
  
  public static void main(String args[]) {

    JFrame frame = new JFrame("Box Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container contentPane = frame.getContentPane();

    JPanel panel = new JPanel();
    //panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ));
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    contentPane.add(panel);

    for (String text : BUTTON_STRINGS) {
      JButton button = new JButton(text);
      
      JPanel btnPane = new JPanel(new BorderLayout());
      btnPane.add(button);
      panel.add(btnPane);
    }

    frame.pack();
    frame.setVisible(true);
  }
}

Last edited by Fubarable; 06-08-2009 at 01:09 PM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-08-2009, 09:08 PM
hardwired's Avatar
Senior Member
 
Join Date: Jul 2007
Posts: 1,577
Rep Power: 4
hardwired is on a distinguished road
Default
Code:
import java.awt.*;
import javax.swing.*;

public class SameHeight {
    private JPanel getContent() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.fill = GridBagConstraints.VERTICAL;
        panel.add(getButton(1, ""), gbc);
        panel.add(getButton(2, "<br>Spring Water"), gbc);
        panel.add(getButton(3, ""), gbc);
        panel.add(getButton(4, "<br>Fountain Water"), gbc);
        panel.add(getButton(5, ""), gbc);
        panel.add(getButton(6, "<br>Moutain Spring Water"), gbc);
        return panel;
    }

    private JButton getButton(int n, String text) {
        String s = "<html><body>Button " + n +
                    text + "</html></body>";
        return new JButton(s);
    }

    public static void main( String args[] ) {
        SameHeight test = new SameHeight();
        JFrame frame = new JFrame("Box Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane();
        contentPane.add(test.getContent());
        frame.pack();
        frame.setVisible(true);
    }
}
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-10-2009, 11:45 AM
Member
 
Join Date: Feb 2009
Posts: 3
Rep Power: 0
PetalumaBoy is on a distinguished road
Default
Thank you very much folks, both solutions work fine.
Fubarable your solution also works with GUI Builder
(Swing Designer) which I am using.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-10-2009, 02:27 PM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,447
Rep Power: 8
Fubarable is on a distinguished road
Default
A different solution is to set the maximum size of the buttons to an appropriately large size.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange behaviour in serialization Wolverine Networking 0 05-23-2009 01:03 PM
[SOLVED] I share BoxLayout with Conteiner but it doesn' t work Wolverine AWT / Swing 7 05-22-2009 04:57 AM
[SOLVED] Possible to have two BoxLayout in same area of BorderLayout? mainy AWT / Swing 4 02-16-2009 10:52 PM
AffinedTransform strange behaviour Echilon AWT / Swing 3 12-11-2008 10:58 AM
Strange behaviour in swing cbalu AWT / Swing 1 05-23-2008 10:23 PM


All times are GMT +2. The time now is 01:24 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org