Java JLabel link mnemonic key with JTextField

Description

Java JLabel link mnemonic key with JTextField

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class Main extends JFrame {

  public Main() {
    super("Link JLabel with JTextField");

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    JTextField nameTextField = new JTextField("Please enter your name...");

    // Create a JLabel with N as its mnemonic 
    //nameTextField as its label-for component
    JLabel nameLabel = new JLabel("Name:");
    nameLabel.setDisplayedMnemonic('N');
    nameLabel.setLabelFor(nameTextField);

    add(nameLabel);//from   w  w  w . j av a2 s  .com
    add(nameTextField,BorderLayout.SOUTH);

  }

  public static void main(String[] args) {
    Main frame = new Main();
    frame.pack();
    frame.setVisible(true);
  }
}



PreviousNext

Related