How do I associate JLabel component with a JTextField?

In this example we associate a JLabel component with JTextField and JPasswordField using the setLabelFor(). A mnemonic need to be set for the JLabel component and when we press the defined key (ALT + U or ALT + P) a text field will be gained focus.

package org.kodejava.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public class JLabelSetForTextField extends JFrame {
    public JLabelSetForTextField() throws HeadlessException {
        initialize();
    }

    private void initialize() {
        setSize(350, 200);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLayout(new FlowLayout(FlowLayout.LEFT));

        JLabel usernameLabel = new JLabel("Username: ");
        JLabel passwordLabel = new JLabel("Password: ");
        JTextField usernameField = new JTextField(20);
        JPasswordField passwordField = new JPasswordField(20);

        // To make the association between the JLabel and JTextField or
        // JPasswordField we need to define the displayed mnemonic and then
        // call JLabel's setLabelFor method.
        usernameLabel.setDisplayedMnemonic(KeyEvent.VK_U);
        usernameLabel.setLabelFor(usernameField);
        passwordLabel.setDisplayedMnemonic(KeyEvent.VK_P);
        passwordLabel.setLabelFor(passwordField);

        getContentPane().add(usernameLabel);
        getContentPane().add(usernameField);
        getContentPane().add(passwordLabel);
        getContentPane().add(passwordField);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new JLabelSetForTextField().setVisible(true));
    }
}
Wayan

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.