1

I am trying to create a Form in Java with Swing and I'm having difficulty with managing my layouts.

I want to have a few text fields with labels in the center of a dialog and have "save" and "close" buttons in the lower right.

Adding the buttons to the lower right of the dialog is simple, but I am not sure how to center align the text fields. I figured that, if there wasn't a center component method, then I might be able to align the field by calculating the center of the dialog window and then updating the position when the dialog is re-sized. But I am new to swing and I'm not sure how to do either (or if that is even a good idea).

How can I center align my components using the Spring Layout Manager?

public class Main {
    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                MyFrame myFrame = new MyFrame();
                myFrame.setVisible(true);
            }
        });
    }
}

Here's how the frame looks:

public class MyFrame extends JFrame {

    JLabel label1;
    JTextField field1;

    JLabel label2;
    JTextField field2;

    JButton saveButton;
    JButton closeButton;

    public MyFrame() {
        initLookAndFeel();
        initFrameProperties();
        initContent();
        initLayout();
    }

    private initContent() {
        label1= new JLabel("Label 1");
        field1= new JTextField();
        label1.setLabelFor(field1);

        label2= new JLabel("Label 2");
        field2= new JTextField();
        label2.setLabelFor(field2);

        saveButton = new JButton("Save");

        closeButton = new JButton("Close");
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });


        this.add(label1);
        this.add(field1);
        this.add(lebel2);
        this.add(field2);
        this.add(saveButton);
        this.add(closeButton);
    }

    private void initLayout() {
        SpringLayout layout = new SpringLayout();

        this.setLayout(layout);
    }
Jamie P
  • 83
  • 2
  • 12
  • 1) For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). 2) Provide ASCII art (or an image with a simple drawing) of the GUI as it should appear in smallest size and (if resizable) with extra width/height. – Andrew Thompson Feb 07 '14 at 07:53
  • @leigero Be sure to add the `@` notation to *notify the person* of a new comment. And in response to your enquiry, it is a shorter version of what the SSCCE was trying to convey, so yes. Further, the MCTaRE is hosted here at Stack Exchange, whereas the SSCCE.org domain that hosts the SSCCE document will be disappearing forever at the end of February. – Andrew Thompson Feb 07 '14 at 08:05

2 Answers2

6

You can center align components by adding a constraint that sets the Horizontal Center of your component to be the same as the Horizontal Center of the content pane. This will automatically update the components position when the window is re-sized.

SpringLayout layout = new SpringLayout();

// For Horizontal Alignment    
layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, component, 0, SpringLayout.HORIZONTAL_CENTER, contentPane);

// For Vertical Alignment
layout.putConstraint(SpringLayout.VERTICAL_CENTER, component, 0, SpringLayout.VERTICAL_CENTER, contentPane);

setLayout(layout);
Jamie P
  • 83
  • 2
  • 12
3
  • SpringLayout.NORTH specifies the top edge of a component's bounding rectangle.
  • SpringLayout.SOUTH specifies the bottom edge of a component's bounding rectangle.
  • SpringLayout.EAST specifies the right edge of a component's bounding rectangle.
  • SpringLayout.WEST specifies the left edge of a component's bounding rectangle.
  • SpringLayout.BASELINE specifies the baseline of a component.
  • SpringLayout.HORIZONTAL_CENTER specifies the horizontal center of a component's bounding rectangle.
  • SpringLayout.VERTICAL_CENTER specifies the vertical center of a component's bounding rectangle.

(How to Use SpringLayout)

Use the same springLayout.putConstraint() for placing at center, as you do in the corner.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • I've already read the page that you linked/copy and pasted from. I'm looking for a way to center align a text field and have it remain centered after resize using the Spring Layout. If I had to guess I'd say you are suggesting I use `SpringLayout.HORIZONTAL_CENTER` like so? `layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, myfield, 0, SpringLayout.HORIZONTAL_CENTER, this);` – Jamie P Feb 09 '14 at 22:31