Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-09-2008, 04:19 AM
rpwtdj's Avatar
Member
 
Join Date: Dec 2007
Posts: 15
rpwtdj is on a distinguished road
Send a message via MSN to rpwtdj
How to add hotkey(Ctrl+VK) to a button?
Hi buddies,
I've added a button to a panel, and used
Code:
setMnemonic(KeyEvent.VK_R)
to set a hotkey Alt+R for triggering the button event.
However, the requirement is changed, now, I need to use 'Ctrl+R' as the
hotkey for the button.
Will you please give me some hit on this?
Thanks!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-09-2008, 06:06 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test { public static void main(String[] args) { JButton button = new JButton(); button.setAction(new AbstractAction("press me") { { putValue(Action.ACTION_COMMAND_KEY, getValue(Action.NAME)); } public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); } }); JPanel panel = new JPanel(); panel.add(button); panel.setPreferredSize(new Dimension(200,100)); // Bind a keystroke to the button to act as accelerator. int c = JComponent.WHEN_IN_FOCUSED_WINDOW; KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_DOWN_MASK); panel.getInputMap(c).put(ks, "PRESS"); panel.getActionMap().put("PRESS", button.getAction()); JOptionPane.showMessageDialog(null, panel,"", -1); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 01-09-2008, 08:28 AM
rpwtdj's Avatar
Member
 
Join Date: Dec 2007
Posts: 15
rpwtdj is on a distinguished road
Send a message via MSN to rpwtdj
Thank you hardwired.
Great help.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 01-09-2008, 09:09 AM
rpwtdj's Avatar
Member
 
Join Date: Dec 2007
Posts: 15
rpwtdj is on a distinguished road
Send a message via MSN to rpwtdj
How can I make the 'r' in "press me" underlined?
so that the button will look like "press me"
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 01-10-2008, 01:58 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Swing labels support primitive html 3.2:
Code:
String s = "<html>p<u>r</u>ess me"; button.setAction(new AbstractAction(s) { { putValue(Action.ACTION_COMMAND_KEY, "press me"); }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 01-10-2008, 03:13 AM
rpwtdj's Avatar
Member
 
Join Date: Dec 2007
Posts: 15
rpwtdj is on a distinguished road
Send a message via MSN to rpwtdj
That would be great!
Thank you
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Open type (Ctrl-Shift-T) does not find types Zhenya_Merom Eclipse 1 08-05-2008 10:54 AM
Run method on Ctrl+Shift+D Echilon AWT / Swing 1 02-16-2008 11:31 PM
Using SWT Button Java Tip Java Tips 0 01-08-2008 10:05 AM
SWT Button JavaForums Java Blogs 0 12-31-2007 05:53 PM
how to issue the command of Ctrl-C (copy) in Java bilal_ali_java Advanced Java 0 07-18-2007 04:14 PM


All times are GMT +3. The time now is 01:10 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org