Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add mnemonics in java swing?

Tags:

java

swing

So I have a greeting program with 3 JTextfields and 3 JLabels next to them and I want to add mnemonics, which I believe is the little underline underneath one of the letters in the JLabel next to the JTextField. When the user presses Alt+underlined key, then the cursor will go to the JTextfield next to it. Here is my code so that just shows the simple greeting without mnemonics

import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class GreetingApp extends JFrame {
    private static final long serialVersionUID = 1L;
    private final JTextField firstNameField,middleNameField,lastNameField;
    private final JButton greetingButton;
    public GreetingApp() {
        super("Greetings");
        this.firstNameField = new JTextField(8);
        this.middleNameField = new JTextField(8);
        this.lastNameField = new JTextField(8);
        this.greetingButton = new JButton("Get Greeting"); 

        greetingButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(final ActionEvent ae){
                showGreeting();
            }
        });
        final Container mainPanel = getContentPane();
        mainPanel.setLayout(new BorderLayout());
        final JPanel inputPanel = new JPanel();
        final JPanel buttonPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(3,3,3,5));
        buttonPanel.setLayout(new FlowLayout());
        JSeparator sep = new JSeparator();
        inputPanel.add(new JLabel("First Name: "),JLabel.LEFT_ALIGNMENT);
        inputPanel.add(firstNameField);
        inputPanel.add(new JLabel("MI: "),JLabel.LEFT_ALIGNMENT);
        inputPanel.add(middleNameField);
        inputPanel.add(new JLabel("Last Name: "),JLabel.LEFT_ALIGNMENT);
        inputPanel.add(lastNameField);
        mainPanel.add(inputPanel,BorderLayout.PAGE_START);
        //buttonPanel.add(sep,BorderLayout.PAGE_START);
        mainPanel.add(sep,BorderLayout.CENTER);
        buttonPanel.add(greetingButton);
        mainPanel.add(buttonPanel,BorderLayout.PAGE_END);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
    private String getFullName() throws IllegalStateException{
        if(firstNameField.getText().trim().length() == 0){
            throw new IllegalArgumentException("First name cannot be blank");
        }

        if(middleNameField.getText().trim().length() > 1){
            throw new IllegalArgumentException("Middle intial cannot be greater than 1 letter");
        }
        if(lastNameField.getText().trim().length() == 0){
            throw new IllegalArgumentException("Last name cannot be blank");
        }
        if(middleNameField.getText().trim().length() ==0){
            return "Greetings, "+this.firstNameField.getText()+" "+ this.middleNameField.getText() +this.lastNameField.getText()+"!";
        }
        return "Greetings, "+this.firstNameField.getText()+" "+ this.middleNameField.getText()+"."+this.lastNameField.getText()+"!";
    }
    private void showGreeting(){

        try{
            String message = getFullName();
            JOptionPane.showMessageDialog(this, message);
       }catch(final IllegalArgumentException iae){
           JOptionPane.showMessageDialog(this,
                   iae.getMessage(),
                   "Error",
                   JOptionPane.ERROR_MESSAGE);
       }

    }
    public static void main(String[] args) {
        try{
            for(LookAndFeelInfo info:UIManager.getInstalledLookAndFeels()){
                if("Nimbus".equals(info.getName())){
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        }catch(Exception e){
            e.getStackTrace();
        }
    }
}
like image 800
user1775500 Avatar asked Apr 29 '13 19:04

user1775500


1 Answers

If your JLabel is called label and your JTextField is called textField:

label.setDisplayedMnemonic(KeyEvent.VK_N); //replace .VK_N with the appropriate key

will place the mnemonic on the label, and:

label.setLabelFor(textField);

will associate that label (and its mnemonic) with the appropriate text field

like image 81
drew moore Avatar answered Oct 29 '22 22:10

drew moore