Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

registerKeyBoard Action with JFrame  RSS feed

 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We all know that registerKeyBoardAction is used with JComponent for mapping shortcut keyboard keys in case mouse stops functioning
I want to do same functionality with JFrame
Can any one give me a crack for it
:roll:
 
Idly Vada
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did this using JRootPane.
use gertRootPane() method of JFrame to get root pane of the JFrame and register keyboard events with root the pane.
code:
import javax.swing.*;
import java.awt.event.*;
class Swingtest extends JFrame {

JRootPane root;

ActionListener actionListener;


public Swingtest() {
actionListener= new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
dispose();
}
};
KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
root=this.getRootPane();

root.registerKeyboardAction (actionListener,stroke,JComponent.WHEN_IN_FOCUSED_WINDOW);
}
public static void main(String args[]) {
System.out.println("Starting Swingtest...");
Swingtest mainFrame = new Swingtest();
mainFrame.setSize(400, 400);
mainFrame.setTitle("Swingtest");
mainFrame.setVisible(true);
}
}
 
Gaurav Chikara
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnkx a lot
You are great
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!