This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

shortcut key for label

 
Swela Jathar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have desktop application.I want to set shortcut key of character to Label.can I use setMnemonic() method on label???or any other solution.

Thank you in advance...
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A JLabel itself doesn't handle mnemonics or shortcuts. You can specify that a label belongs to a component with the setLabelFor method. After that you can use setDisplayedMnemonic or setDisplayedMnemonicIndex to set the mnemonic for the label. This will however only request that the connected component (as specified in setLabelFor) will receive focus / be the currently selected component. It will not trigger anything.

I'm not sure if JLabel can handle key bindings. If so, you can perhaps use that.
 
Swela Jathar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for reply........
You can specify that a label belongs to a component with the setLabelFor method. After that you can use setDisplayedMnemonic or setDisplayedMnemonicIndex to set the mnemonic for the label. This will however only request that the connected component (as specified in setLabelFor) will receive focus / be the currently selected component. It will not trigger anything.


but which component is convenient for this method???can you give me example??
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pressing Alt+E will now select the textField field.
 
Swela Jathar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks rob....
but i want to call another form from label.Means i want to open form when i press key.
please help.
 
Rob Camick
Ranch Hand
Posts: 2539
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to open form when i press key


Then you should be using a JButton, not a label. You can add an ActionListener to the button and also set the mnemonic of the button.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic