Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Install keystroke on action without using a JMenuItem

Thread view: 
Bart Cremers - 30 Dec 2005 17:10 GMT
Hi,

I need to install an Action with a defined accelerator key without
using a corresponding JMenuItem. I've got a JButton to correspond with
the accelerator key, but no Menu item.

I've been looking at the code for JMenuItem and I found the code in
"installAction" in BasicMenuItemUI, but installing the keystroke this
way does not fire the action.

Anyone knows how to get this working?

Regards,

Bart

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.*;

public class Test extends JFrame {

   @Override
   protected void frameInit() {
       super.frameInit();
       setLayout(new FlowLayout());
       AAction acA = new AAction();
       JButton btA = new JButton(acA);
       installAction(btA, acA);

       BAction acB = new BAction();
       JButton btB = new JButton(acB);
       installAction(btB, acB);

       add(btA);
       add(btB);
   }

   public static void main(String[] args) {
       Test t = new Test();

       t.setDefaultCloseOperation(EXIT_ON_CLOSE);
       t.pack();
       t.setLocationRelativeTo(null);
       t.setVisible(true);
   }

   class AAction extends AbstractAction {
       AAction() {
           super("A");
           putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl
A"));
       }

       public void actionPerformed(ActionEvent e) {
           System.out.println("Test$AAction.actionPerformed");
       }
   }

   class BAction extends AbstractAction {

       BAction() {
           super("B");
           putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("ctrl
B"));
       }

       public void actionPerformed(ActionEvent e) {
           System.out.println("Test$BAction.actionPerformed");
       }
   }

   public static void installAction(JComponent c, Action a) {
       KeyStroke accelerator = (KeyStroke)
a.getValue(Action.ACCELERATOR_KEY);
       InputMap windowInputMap = SwingUtilities.getUIInputMap(c,

JComponent.WHEN_IN_FOCUSED_WINDOW);

       if (windowInputMap != null) {
           windowInputMap.clear();
       }
       if (accelerator != null) {
           if (windowInputMap == null) {
               windowInputMap = new ComponentInputMap(c);
               SwingUtilities.replaceUIInputMap(c,
JComponent.WHEN_IN_FOCUSED_WINDOW,
                                                windowInputMap);
           }
           windowInputMap.put(accelerator, "doClick");
       }
   }
}
Andrey Kuznetsov - 31 Dec 2005 15:32 GMT
> I need to install an Action with a defined accelerator key without
> using a corresponding JMenuItem. I've got a JButton to correspond with
> the accelerator key, but no Menu item.

JComponent.registerKeyboardAction();
http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Andrey Kuznetsov - 31 Dec 2005 23:18 GMT
> JComponent.registerKeyboardAction();
> http://java.sun.com/products/jfc/tsc/special_report/kestrel/keybindings.html

sorry, this is old obsolete method.

use a combination of getActionMap() and getInputMap():
if you want to bind Action a to KeyStroke ks, then use following:

String command = "<whatever>";
int condition; //one of WHEN_IN_FOCUSED_WINDOW, WHEN_FOCUSED,
WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
component.getInputMap(condition).put(ks, command);
component.getActionMap().put(commmand, a);

Signature

Andrey Kuznetsov
http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.