Java Swing GUI: Moving around components specifically with layouts

I'm making a little test GUI for something I'm making. However, problems occur with the positioning of the panels.

public winInit() {
    super("Chatterbox - Login");

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch(ClassNotFoundException e) {
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    } catch (UnsupportedLookAndFeelException e) {
    }

    setSize(300,135);

    pn1 = new JPanel();
    pn2 = new JPanel();
    pn3 = new JPanel();

    l1 = new JLabel("Username");
    l2 = new JLabel("Password");
    l3 = new JLabel("Random text here");
    l4 = new JLabel("Server Address");
    l5 = new JLabel("No address set.");

    i1 = new JTextField(10);

    p1 = new JPasswordField(10);

    b1 = new JButton("Connect");
    b2 = new JButton("Register");
    b3 = new JButton("Set IP");

    l4.setBounds(10, 12, getDim(l4).width, getDim(l4).height);
    l1.setBounds(10, 35, getDim(l1).width, getDim(l1).height);
    l2.setBounds(10, 60, getDim(l2).width, getDim(l2).height);
    l3.setBounds(10, 85, getDim(l3).width, getDim(l3).height);
    l5.setBounds(l4.getBounds().width + 14, 12, l5.getPreferredSize().width, l5.getPreferredSize().height);

    l5.setForeground(Color.gray);

    i1.setBounds(getDim(l1).width + 15, 35, getDim(i1).width, getDim(i1).height);
    p1.setBounds(getDim(l1).width + 15, 60, getDim(p1).width, getDim(p1).height);

    b1.setBounds(getDim(l1).width + getDim(i1).width + 23, 34, getDim(b2).width, getDim(b1).height - 5);
    b2.setBounds(getDim(l1).width + getDim(i1).width + 23, 60, getDim(b2).width, getDim(b2).height - 5);
    b3.setBounds(getDim(l1).width + getDim(i1).width + 23, 10, etDim(b2).width, getDim(b3).height - 5);

    b1.addActionListener(clickButton);
    b2.addActionListener(clickButton);
    b3.addActionListener(clickButton);

    pn1.setLayout(new FlowLayout(FlowLayout.RIGHT));
    pn2.setLayout(new FlowLayout(FlowLayout.RIGHT));

    pn1.add(l1);
    pn1.add(i1);
    pn1.add(b1);

    pn2.add(l2);
    pn2.add(p1);
    pn2.add(b2);

    add(pn1);
    add(pn2);

}

I am attempting to use FlowLayout to position the panels in the way desired. I'd use BorderLayout while adding, but the vertical spacing is too far away when I just use directions closest to one another.

The output of this code is to create a window, 300,150, place whatever's in the two panels in the exact same spaces.

Yes, I realize there's useless code there with setBounds(), but that was just me screwing around with Absolute Positioning, which wasn't working out for me either.

EDIT:

Question resolved...I think. I'm quite happy using MiGLayout right now, but I'm also seeing what the GridBagLayout has to offer as well. Seeing from some of your guys' examples, it seems very similar to what I'm trying to attain.

Thank you for your answers.

Upvotes: 1

Views: 2127

Answers (3)

MadProgrammer
MadProgrammer

Reputation: 347204

From reading the code, it would seem that you're after a form that looks something like this...

Like this

I got this together by using GridBagLayout

l1 = new javax.swing.JLabel();
l2 = new javax.swing.JLabel();
l3 = new javax.swing.JLabel();
l4 = new javax.swing.JLabel();
l5 = new javax.swing.JLabel();
i1 = new javax.swing.JTextField();
p1 = new javax.swing.JPasswordField();
b1 = new javax.swing.JButton();
b2 = new javax.swing.JButton();
b3 = new javax.swing.JButton();

setLayout(new java.awt.GridBagLayout());

l4.setText("Server Address");
gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.LINE_START;
gbc.insets = new java.awt.Insets(2, 2, 2, 2);
add(l4, gbc);

l1.setText("Username");
gbc.gridy = 1;
add(l1, gbc);

l2.setText("Password");
gbc.gridy = 2;
add(l2, gbc);

l3.setText("Random text here");
gbc.gridy = 3;
add(l3, gbc);

l5.setText("No address set");
gbc.gridx = 1;
gbc.gridy = 0;
add(l5, gbc);

i1.setColumns(12);
gbc.gridy = 1;
add(i1, gbc);

p1.setColumns(12);
gbc.gridy = 2;
add(p1, gbc);

b3.setText("Set IP");
gbc.gridx = 2;
gbc.gridy = 0;
add(b3, gbc);

b1.setText("Connect");
gbc.gridx = 2;
gbc.gridy = 1;
add(b1, gbc);

b2.setText("Register");
gbc.gridy = 2;
add(b2, gbc);

Upvotes: 3

Mike
Mike

Reputation: 1899

I do not know what you exactly wants, but this should serve your purpose.

import java.awt.BorderLayout;
public class loginBox extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private JTextField txtUserName;
    private JPasswordField passwordField;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            loginBox dialog = new loginBox();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public loginBox() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new TitledBorder(null, "Log In", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.Y_AXIS));
        {
            JPanel panel = new JPanel();
            contentPanel.add(panel);
            panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
            {
                JLabel lblChatterBoxlogin = new JLabel("Chatter Box-Login");
                panel.add(lblChatterBoxlogin);
            }
        }
        {
            JPanel formPanel = new JPanel();
            contentPanel.add(formPanel);
            GridBagLayout gbl_formPanel = new GridBagLayout();
            gbl_formPanel.columnWidths = new int[]{52, 0, 0, 0};
            gbl_formPanel.rowHeights = new int[]{14, 0, 0, 0, 0, 0};
            gbl_formPanel.columnWeights = new double[]{0.0, 1.0, 1.0, Double.MIN_VALUE};
            gbl_formPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
            formPanel.setLayout(gbl_formPanel);
            {
                JLabel lblUserName = new JLabel("User Name");
                GridBagConstraints gbc_lblUserName = new GridBagConstraints();
                gbc_lblUserName.anchor = GridBagConstraints.WEST;
                gbc_lblUserName.insets = new Insets(0, 0, 5, 5);
                gbc_lblUserName.gridx = 0;
                gbc_lblUserName.gridy = 0;
                formPanel.add(lblUserName, gbc_lblUserName);
            }
            {
                txtUserName = new JTextField();
                GridBagConstraints gbc_txtUserName = new GridBagConstraints();
                gbc_txtUserName.insets = new Insets(0, 0, 5, 5);
                gbc_txtUserName.fill = GridBagConstraints.HORIZONTAL;
                gbc_txtUserName.gridx = 1;
                gbc_txtUserName.gridy = 0;
                formPanel.add(txtUserName, gbc_txtUserName);
                txtUserName.setColumns(10);
            }
            {
                JLabel lblPassword = new JLabel("Password");
                GridBagConstraints gbc_lblPassword = new GridBagConstraints();
                gbc_lblPassword.anchor = GridBagConstraints.WEST;
                gbc_lblPassword.insets = new Insets(0, 0, 5, 5);
                gbc_lblPassword.gridx = 0;
                gbc_lblPassword.gridy = 1;
                formPanel.add(lblPassword, gbc_lblPassword);
            }
            {
                passwordField = new JPasswordField();
                GridBagConstraints gbc_passwordField = new GridBagConstraints();
                gbc_passwordField.insets = new Insets(0, 0, 5, 5);
                gbc_passwordField.fill = GridBagConstraints.HORIZONTAL;
                gbc_passwordField.gridx = 1;
                gbc_passwordField.gridy = 1;
                formPanel.add(passwordField, gbc_passwordField);
            }
            {
                JLabel lblServer = new JLabel("Server Address");
                GridBagConstraints gbc_lblServer = new GridBagConstraints();
                gbc_lblServer.anchor = GridBagConstraints.ABOVE_BASELINE_TRAILING;
                gbc_lblServer.insets = new Insets(0, 0, 5, 5);
                gbc_lblServer.gridx = 0;
                gbc_lblServer.gridy = 2;
                formPanel.add(lblServer, gbc_lblServer);
            }
            {
                textField = new JTextField();
                GridBagConstraints gbc_textField = new GridBagConstraints();
                gbc_textField.insets = new Insets(0, 0, 5, 5);
                gbc_textField.fill = GridBagConstraints.HORIZONTAL;
                gbc_textField.gridx = 1;
                gbc_textField.gridy = 2;
                formPanel.add(textField, gbc_textField);
                textField.setColumns(10);
            }
            {
                JLabel lblNoAddressSet = new JLabel("No Address Set");
                GridBagConstraints gbc_lblNoAddressSet = new GridBagConstraints();
                gbc_lblNoAddressSet.insets = new Insets(0, 0, 5, 0);
                gbc_lblNoAddressSet.gridx = 2;
                gbc_lblNoAddressSet.gridy = 2;
                formPanel.add(lblNoAddressSet, gbc_lblNoAddressSet);
            }
            {
                JLabel lblRandomTextHere = new JLabel("Random Text Here");
                GridBagConstraints gbc_lblRandomTextHere = new GridBagConstraints();
                gbc_lblRandomTextHere.insets = new Insets(0, 0, 5, 5);
                gbc_lblRandomTextHere.gridx = 1;
                gbc_lblRandomTextHere.gridy = 3;
                formPanel.add(lblRandomTextHere, gbc_lblRandomTextHere);
            }
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("Connect");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton btnNewButton = new JButton("Register");
                buttonPane.add(btnNewButton);
            }
            {
                JButton btnNewButton_1 = new JButton("Set IP");
                buttonPane.add(btnNewButton_1);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

}

Upvotes: -1

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

If you must do absolute positioning (and I don't recommend this except for animation), then the layout needs to be set to null, not FlowLayout.

For data acquisition panels, I recommend using a smart combination of layout managers including considering some of the non-standard layouts such as MiGLayout.

Edit for clarification: MiGLayout is probably easier to use than GridBagLayout, but if you get familiar with GridBagLayout, it's not that hard to use, and it doesn't require a download since it's part of the core Java library. Myself, I'd likely use nested JPanels that use a combination of layouts, perhaps BorderLayout for the overall GUI and GridBagLayout for the data acquisition panel, the one with rows of JLabel / JTextField combinations.

Upvotes: 3

Related Questions