Positioning

Asked

Viewed 121 times

0

I’m having a hard time adding Abels in different positions, relative to a field (JTextField). I’d like to put one label before the field, the left, and a second label under the JTextField.

Example: inserir a descrição da imagem aqui

I tried to use some layout managers, including the GridBagConstraints, however, not being able to do what I wanted. Someone could give me at least one direction how to do ?

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TesteLabel extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(()
                -> {
            TesteLabel testeLabel = new TesteLabel();
        });
    }

    public TesteLabel() {
        add(addComponent());
        setSize(500, 200);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private JComponent addComponent() {
        JPanel painelPrincipal = new JPanel();

        JPanel painel = new JPanel();
        painel.setLayout(new FlowLayout());

        JLabel labelLateral = new JLabel("Label Lateral: ");
        JLabel labelInferior = new JLabel("Label Inferior: ");

        JTextField jTextField = new JTextField();

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = 1;
        gbc.gridx = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.SOUTH;
        gbc.insets = new Insets(5, 5, 5, 5);

        //painel.add(labelLateral, jTextField);
        painel.add(labelLateral);
        painel.add(jTextField);
        painel.add(labelInferior, gbc);
        jTextField.setPreferredSize(new Dimension(200, 25));

        painelPrincipal.add(painel);

        return painelPrincipal;
    }
}

1 answer

1


I got it this way:

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TesteLabel extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(TesteLabel::new);
    }

    public TesteLabel() {
        add(addComponent());
        setSize(500, 200);
        setVisible(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private JComponent addComponent() {
        JPanel painelPrincipal = new JPanel();

        JPanel painel = new JPanel();
        painel.setLayout(new GridBagLayout());

        JLabel labelLateral = new JLabel("Label Lateral: ");

        GridBagConstraints gbc1 = new GridBagConstraints();
        gbc1.weightx = 1.0;
        gbc1.weighty = 1.0;
        gbc1.gridx = 1;
        gbc1.gridy = 1;
        painel.add(labelLateral, gbc1);

        JTextField jTextField = new JTextField();

        GridBagConstraints gbc2 = (GridBagConstraints) gbc1.clone();
        gbc2.gridx = 2;
        gbc2.fill = GridBagConstraints.BOTH;
        gbc2.gridwidth = GridBagConstraints.REMAINDER;

        painel.add(jTextField, gbc2);

        JLabel labelInferior = new JLabel("Label Inferior: ");

        GridBagConstraints gbc3 = (GridBagConstraints) gbc1.clone();
        gbc3.gridx = 2;
        gbc3.gridy = 2;
        gbc3.insets = new Insets(5, 5, 5, 5);

        painel.add(labelInferior, gbc3);

        painelPrincipal.add(painel);

        return painelPrincipal;
    }
}

Upshot:

inserir a descrição da imagem aqui

The secret here was to use gridX and gridY as coordinates to position the components, and the constant GridBagConstraints.REMAINDER to force the text field to be the last entry on that line. I used GridBagConstraints.BOTH so that the text field fills the entire space of that linhaxcolune in which he finds himself, without which he will not expand.

However, it is not possible to leave that space in the second row with this layout, because for that, you would need to have a component there, occupying the space for a column 3. As the panel only has 2 columns, the lower label will be in the last column.

You can see more about this layout in this oracle tutorial.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.