Use code like this
JFrame frame = new JFrame();
JPanel pnl = new JPanel();
pnl.addKeyListener(keyListener);
pnl.setFocusable(true);
frame.add(pnl);
frame.pack();
pnl.drawing.requestFocus();
Showing posts with label KeyListener. Show all posts
Showing posts with label KeyListener. Show all posts
2008-06-23
2008-05-20
Add KeyListener on Java Swing JFrame
Use a code like this
AbstractAction act = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//Action that you want to do.
}
};
getRootPane().getActionMap().put("nameThatYouWant", act);
InputMap im = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
// KeyEvent.VK_F10 or any key that you want.
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "nameThatYouWant");
AbstractAction act = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
//Action that you want to do.
}
};
getRootPane().getActionMap().put("nameThatYouWant", act);
InputMap im = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
// KeyEvent.VK_F10 or any key that you want.
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "nameThatYouWant");
Subscribe to:
Posts (Atom)