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

1
  • "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. Aug 10, 2011 at 2:33

4 Answers 4

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

2
  • 1
    always there and very fast +1
    – MByD
    Aug 9, 2011 at 18:52
  • 2
    +1 for BoxLayout w/example. See also this answer, which adds labels.
    – trashgod
    Aug 9, 2011 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.

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.