Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I add mnemonics in java swing?
    text
    copied!<p>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</p> <pre><code>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() &gt; 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(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload