Google Groups

Re: How to align swing buttons vertically ?


John B. Matthews Nov 13, 2011 2:33 PM
Posted in group: comp.lang.java.programmer
In article <4ec02213$0$5039$ba62...@news.skynet.be>,
 Olivier Scalbert <olivier....@algosyn.com> wrote:

> With "my version", I have (at least on my machine):
> -----------------
> |   button 1    |
> -----------------
> |   button 2    |
> -----------------
> | Long Button 3 |
> -----------------
> |   button 4    |
> -----------------
> |   button 5    |
> -----------------
>
> With "yours" (at least the one I post with the BoxLayout):
>
> ------------
> | button 1 |
> ------------
> | button 2 |
> -----------------
> | Long Button 3 |
> -----------------
> | button 4 |
> ------------
> | button 5 |
> ------------

Lew is correct about using pack(), and I'm with Martin on BoxLayout.
One problem with using setXXXSize is that the values statically replace
those computed by the component's Look & Feel. Instead override just
the one needed by BoxLayout to make the buttons "as wide as their
container."

<http://download.oracle.com/javase/tutorial/uiswing/layout/box.html>

As an aside, your center panel contains no components, so it would
pack() to the L&F's minimum. I've given it an arbitrary size by
overriding getPreferredSize(), but setPreferredSize() would do as well
for the fixed value.

I defer to Knute on GridBagLayout.

Here's an <http://sscce.org/>:

import java.awt.*;
import javax.swing.*;

public class TestViewer {
   
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
           
            @Override
            public void run() {
                JFrame frame = new ViewerFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

class ViewerFrame extends JFrame {
   
    public ViewerFrame() {
        // a panel of aribtrary size
        this.add(new JPanel() {
           
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        }, BorderLayout.CENTER);
        this.add(createBtnPanel(), BorderLayout.EAST);
    }
   
    private JPanel createBtnPanel() {
        JPanel btnPanel = new JPanel();
        btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS));
        btnPanel.add(createButton("Button 1"));
        btnPanel.add(createButton("Button 2"));
        btnPanel.add(createButton("Long Button 3"));
        btnPanel.add(createButton("Button 4"));
        btnPanel.add(createButton("Button 5"));
        return btnPanel;
    }
   
    private JButton createButton(String name) {
        final JButton b = new JButton(name) {
           
            @Override
            public Dimension getMaximumSize() {
                return new Dimension(
                    Short.MAX_VALUE, getPreferredSize().height);
            }
        };
        b.setAlignmentX(0.5f);
        return b;
    }
}

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>