Results 1 to 5 of 5
  1. #1
    PetalumaBoy is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    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);
    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    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:

    Java 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 12:09 PM.

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java 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);
        }
    }

  4. #4
    PetalumaBoy is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    Thank you very much folks, both solutions work fine.
    Fubarable your solution also works with GUI Builder
    (Swing Designer) which I am using.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    A different solution is to set the maximum size of the buttons to an appropriately large size.

Similar Threads

  1. Strange behaviour in serialization
    By Wolverine in forum Networking
    Replies: 0
    Last Post: 05-23-2009, 12:03 PM
  2. Replies: 7
    Last Post: 05-22-2009, 03:57 AM
  3. Replies: 4
    Last Post: 02-16-2009, 09:52 PM
  4. AffinedTransform strange behaviour
    By Echilon in forum AWT / Swing
    Replies: 3
    Last Post: 12-11-2008, 09:58 AM
  5. Strange behaviour in swing
    By cbalu in forum AWT / Swing
    Replies: 1
    Last Post: 05-23-2008, 09:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •