• Post Reply Bookmark Topic Watch Topic
  • New Topic

Add mnemonic to text field

 
David Henstridge
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been able to add mnemonics to buttons and they work.

Would someone here be able to tell me how to add a mnemonic for a text field?

The label for the text field allows for a mnemonic, but it has no function.
That is, when I press alt-m etc. there is no effect.

The properties tab for the JTextField does not mention mnemonic.

Advice on how to add these mnemonics would be appreciated.

TIA
 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The label for the text field allows for a mnemonic, but it has no function.  That is, when I press alt-m etc. there is no effect.


This will just place focus on the text field.

What are you expecting to happen when you use a mnemonic?

Typically you add an ActionListener to a JTextField. The ActionListener would be invoked when the text field has focus and the Enter key is pressed.
 
David Henstridge
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:
The label for the text field allows for a mnemonic, but it has no function.  That is, when I press alt-m etc. there is no effect.


This will just place focus on the text field.

What are you expecting to happen when you use a mnemonic?

Typically you add an ActionListener to a JTextField. The ActionListener would be invoked when the text field has focus and the Enter key is pressed.


I simply would like to give focus to the text field when the mnemonic is pressed.

You mentioned above that this is what should take place, but it is not happening.
I noted that the text label had provision to state a mnemonic, but the JTextField
itself did not mention any mnemonic.

 
Rob Camick
Ranch Hand
Posts: 2700
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to also use:



so the label knows which component to focus on when the mnemonic is used.
 
David Henstridge
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:You need to also use:



so the label knows which component to focus on when the mnemonic is used.


Thanks Rob!
I couldn't find label.setLabelFor or where to put it, but I did find
labelFor under Properties for the JLabel.

I selected txtEmail from the list (the name of the JTextField)
and lo and behold, the mnemonic then worked! 

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!