4

How to align these JComponents like a Form on center of the Content pane...using Swing

        panel1.add(l1);
        panel1.add(c1);
        panel1.add(l2);
        panel1.add(c2);
        panel1.add(b4);
        panel1.add(b5);
        frame1.getContentPane().add(panel1);

Please help me

mKorbel
  • 109,525
  • 20
  • 134
  • 319
ram
  • 41
  • 2
  • *"l1.. c1"* Gee. Can you possibly vague that up for me? Are the l's labels, the c's combos., the b's buttons? Also draw some ASCII art or an image to show the intended layout. Forms can be laid out a lot of different ways. I suspect a nested layout or `GroupLayout` would be best for this, but I won't be looking into it without more information. – Andrew Thompson Aug 10 '11 at 02:33

4 Answers4

6

How about you read the Laying Out Components Within a Container tutorial first? I abuse this saying but, there's always more than one way to skin a cat


Here's a superfluous example that uses BoxLayout and setAlignmentX(...) on the JComponent instances -

public final class StackComponentsDemo {
    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();             
            }
        });
    }

    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        panel.add(new DisabledJButton());
        panel.add(new DisabledJButton());
        panel.add(new DisabledJButton());
        panel.add(new DisabledJButton());
        panel.add(new DisabledJButton());

        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class DisabledJButton extends JButton{
        public DisabledJButton(){
            super("Disabled");
            setEnabled(false);
            setAlignmentX(Component.CENTER_ALIGNMENT);
        }
    }
}

enter image description here

mre
  • 43,520
  • 33
  • 120
  • 170
  • 1
    always there and very fast +1 – MByD Aug 09 '11 at 18:52
  • 2
    +1 for `BoxLayout` w/example. See also this [answer](http://stackoverflow.com/questions/3174765/variable-layout-in-swing/3175280#3175280), which adds labels. – trashgod Aug 09 '11 at 21:58
2

Check SpringLayout if it fits your needs. If not, probably GridBagLayout will do.

Here's an example of using SpringLayout for simple form-like layout.

Michał Šrajer
  • 30,364
  • 7
  • 62
  • 85
1

I would recommend using the boxlayout manager. Using the layout managers without the help of an IDE takes some time and practice. More information can be found here: http://download.oracle.com/javase/tutorial/uiswing/layout/box.html

Menefee
  • 1,475
  • 1
  • 17
  • 26
0

If all else fails you can always try: http://code.google.com/intl/nl-NL/javadevtools/wbpro/layoutmanagers/swing/index.html

I was really impressed with window builder, it generates nice and clean code and is easily integrated into eclipse.