adamb53
adamb53

Reputation: 17

Specifying a certain amount of space between JLabels height in Java

I have tried searching for this answer and tried a few things that I found in posts on here but nothing seems to be working.

What I have is this:

Screenshot of current dialog

with this:

private class InputGUI extends JFrame implements ActionListener
{

    private JTextField urlIn = new JTextField(colWidth);
    private JLabel urlLabel = new JLabel("DB URL: ");

    private JTextField protocolIn = new JTextField(colWidth);
    private JLabel protocolLabel = new JLabel("Protocol: ");

    private JTextField portIn = new JTextField(colWidth);
    private JLabel portLabel = new JLabel("Port: ");

    private JTextField userIn = new JTextField(colWidth);
    private JLabel userLabel = new JLabel("Username: ");

    private JPasswordField passIn = new JPasswordField(colWidth);
    private JLabel passLabel = new JLabel("Password: ");

    private JTextField tableIn = new JTextField(colWidth);
    private JLabel tableLabel = new JLabel("Table: ");


    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public void inputFields()
    {
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                inputExit.doClick();
            }
        });

        // Sets the size, what to do when closed and the title
        this.setSize(350,400);
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        this.setTitle("Brewberry Pi!");

        // Set the location off the frame
        this.setLocationRelativeTo(null);

        button1 = new JButton("Submit");
        button1.addActionListener(this);

        button2 = new JButton("Cancel");
        button2.addActionListener(this);

        inputExit = new JButton("Exit");
        inputExit.addActionListener(this);

        // Panel for content to be added to
        JPanel labelPanel = new JPanel();
        JPanel fieldPanel = new JPanel();
        JPanel buttonPanel = new JPanel();

        Box box1 = Box.createVerticalBox();
        Box box2 = Box.createVerticalBox();

        box1.add(urlLabel);
        box1.add(protocolLabel);
        box1.add(portLabel);
        box1.add(userLabel);
        box1.add(passLabel);
        box1.add(tableLabel);

        box2.add(urlIn);
        box2.add(protocolIn);
        box2.add(portIn);
        box2.add(userIn);
        box2.add(passIn);
        box2.add(tableIn);

        labelPanel.add(box1);
        fieldPanel.add(box2);
        buttonPanel.add(button1);
        buttonPanel.add(button2);   

        // Add Panel to frame
        this.add(labelPanel, BorderLayout.WEST);
        this.add(fieldPanel, BorderLayout.CENTER);
        this.add(buttonPanel, BorderLayout.SOUTH);

        this.pack();

        // Don't allow the user to change the size of our window
        this.setResizable(false);

        // Set to visible
        this.setVisible(true);
    }

I need the labels to line up with the fields but no matter what I try the labels always end up right underneath each other.

Upvotes: 0

Views: 3146

Answers (3)

Janos
Janos

Reputation: 1

GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(20, 20, 20, 20); //here you specify the space between
panel0.add(label0, c);                //top, right, bottom and left

It works for me on labels.

Upvotes: 0

Ankit Rustagi
Ankit Rustagi

Reputation: 5637

Try this:

box1.add(urlLabel);
box1.add(Box.createRigidArea(new Dimension(5,5))); // change these dimension to achieve the correct spacing
box1.add(protocolLabel);
box1.add(Box.createRigidArea(new Dimension(5,5))); 
box1.add(portLabel);
box1.add(Box.createRigidArea(new Dimension(5,5))); 
box1.add(userLabel);
box1.add(Box.createRigidArea(new Dimension(5,5))); 
box1.add(passLabel);
box1.add(Box.createRigidArea(new Dimension(5,5))); 
box1.add(tableLabel);

See also: How to Use BoxLayout: Using Invisible Components as Filler.

Upvotes: 2

kiheru
kiheru

Reputation: 6618

You should use a grid based layout manager. Here's an example of the grid part using GridBagLayout:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GridExample extends JPanel {
    GridExample() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridy = 0;
        add(new JLabel("DB URL: "), gbc);
        gbc.gridy++;
        add(new JLabel("Protocol: "), gbc);
        gbc.gridy++;
        add(new JLabel("Port: "), gbc);
        gbc.gridy++;
        add(new JLabel("Username: "), gbc);
        gbc.gridy++;
        add(new JLabel("Password: "), gbc);
        gbc.gridy++;
        add(new JLabel("Table: "), gbc);
        gbc.gridy = 0;
        gbc.gridx = 1;
        add(new JTextField(16), gbc);
        gbc.gridy++;
        add(new JTextField(16), gbc);
        gbc.gridy++;
        add(new JTextField(16), gbc);
        gbc.gridy++;
        add(new JTextField(16), gbc);
        gbc.gridy++;
        add(new JPasswordField(16), gbc);
        gbc.gridy++;
        add(new JTextField(16), gbc);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("sample");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);
                frame.add(new GridExample());
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

This results in:

Sample image

There are easier to use grid based layout managers than GridBag, but that one has the advantage of being in the standard library. If you end up doing up lots of such layouts, it's worth it looking in to third party layout managers, such as MigLayout or DesignGridLayout.

Upvotes: 2

Related Questions