mlethys
mlethys

Reputation: 436

Putting JLabel on top of component

I need to add JLabel object to specific component of my panel. I use setLabelFor method but this one adds label nex to component. How to change it to set label on top of component?

TextField sampleField = new TextField();
JLabel sampleLabel = new JLabel("sample text");
panel.add(sampleField);
sampleLabel.setLabelFor(sampleField);
panel.add(sampleLabel);

^ this one puts sampleLabel next to sampleField. How to put sampleLabel on top of sampleField?

Thanks from advance.

@Edit:

I use something like this:

 public TabBody()
{
    setLayout(new BorderLayout());
    nameField = new TextField();
    //nameField.setSize(new Dimension(200, 200));
    //revalidate();
    nameLabel = new JLabel("name test");
    amountLabel = new JLabel("amount test");
    amountField = new TextField();
    unitsBox = new JComboBox(units);
    unitsBox.setSelectedIndex(3);

    nameLabel.setLabelFor(nameField);
    amountLabel.setLabelFor(amountField);
    add(nameLabel, BorderLayout.NORTH);
    add(amountLabel, BorderLayout.NORTH);

    add(nameField);
    add(amountField);
    add(unitsBox);
}

and outcome is:

enter image description here

And I need something like this:

enter image description here

Upvotes: 0

Views: 3399

Answers (2)

alex2410
alex2410

Reputation: 10994

Your JPanel use FlowLayout by default, that place component one by one in a row. Because of you have that effect.

You need to use a proper LayoutManager for example use BorderLayout :

import java.awt.BorderLayout;
import java.awt.TextField;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Example {

    public static void main(String[] args){
        JFrame frame = new JFrame();

        JPanel panel = new JPanel(new BorderLayout());
        TextField sampleField = new TextField();
        JLabel sampleLabel = new JLabel("sample text");
        panel.add(sampleField);
        sampleLabel.setLabelFor(sampleField);
        panel.add(sampleLabel,BorderLayout.NORTH);

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

}

enter image description here

If you need to place components in grid, one by one for example try to use GridBagLayout

    JPanel panel = new JPanel(new GridBagLayout());
    JTextField sampleField = new JTextField(5);
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.WEST;
    c.gridx = 0;
    c.gridy = 0;
    c.insets = new Insets(5, 5, 5, 5);

    JLabel sampleLabel = new JLabel("sample text");
    panel.add(sampleLabel,c);

    c.gridy++;
    panel.add(sampleField,c);

    c.gridy++;
    panel.add(new JLabel("sample text 2"),c);

    c.gridy++;
    panel.add(new JTextField(5),c);

    c.gridy++;
    panel.add(new JTextField(5),c);

manage positions with gridx and gridy properties of GridBagConstraints

enter image description here.

Upvotes: 1

Aditya
Aditya

Reputation: 1344

You must use Layout for arranging your components. For JPanel by deafult it is FlowLayout which arranges your component next to one-another. You can use GridLayout or GridBagLayout

Here is the sample code:

TextField sampleField = new TextField();
JLabel sampleLabel = new JLabel("sample text");
panel.setLayout(new GridBagLayout());
sampleLabel.setLabelFor(sampleField);
GridBagConstraints = new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0);
panel.add(sampleLabel, gbc);
gbc.gridY=1;
panel.add(sampleField, gbc);

Upvotes: 0

Related Questions