How to create hot key?

Hi all,

I'm creating an application by using the J2SE v 1.4.2_10 SDK with NetBeans 4.1 Bundle. I have created 4 button and handles event each. I would like to create a hotkey for each button, "F2" key for jButtton2, "F3" key for jButton3, "F8" key for jButton4, "F1" key for jButton5, once i click on the key, the event will be triggered and the coding inside will be executed.

Actually I have read through a few includingHow to Write a Key Listener, How to Write a Component Listener, How to Write an Action Listener, How to Use Actions, How to Use Key Bindings... but i really not so clear about that and don't know how to apply that into my task... Hope someone can help me...Thanks

The following is part of my coding...

/*

* RegisterStudent_ADD.java

*

* Created on November 15, 2005, 11:12 PM

*/

import java.awt.*;

import java.sql.*;

import javax.swing.*;

import java.lang.*;

publicclass RegisterStudent_ADDextends javax.swing.JFrame{

/** Creates new form RegisterStudent_ADD */

public RegisterStudent_ADD(){

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

privatevoid initComponents(){

jDialog1 =new javax.swing.JDialog();

jOptionPane1 =new javax.swing.JOptionPane();

jLabel7 =new javax.swing.JLabel();

jLabel6 =new javax.swing.JLabel();

jLabel5 =new javax.swing.JLabel();

jLabel4 =new javax.swing.JLabel();

jLabel2 =new javax.swing.JLabel();

jLabel8 =new javax.swing.JLabel();

jTextField2 =new javax.swing.JTextField();

jTextField3 =new javax.swing.JTextField();

jTextField4 =new javax.swing.JTextField();

jTextField5 =new javax.swing.JTextField();

jButton5 =new javax.swing.JButton();

jSeparator2 =new javax.swing.JSeparator();

jLabel1 =new javax.swing.JLabel();

jSeparator1 =new javax.swing.JSeparator();

jButton2 =new javax.swing.JButton();

jButton3 =new javax.swing.JButton();

jButton4 =new javax.swing.JButton();

jLabel3 =new javax.swing.JLabel();

jDialog1.getContentPane().setLayout(null);

jDialog1.getContentPane().add(jOptionPane1);

jOptionPane1.setBounds(0, 0, 262, 90);

getContentPane().setLayout(null);

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

setTitle("Register Student - ADD");

jLabel7.setText("Student Phone No");

getContentPane().add(jLabel7);

jLabel7.setBounds(5, 180, 150, 14);

jLabel6.setText("Student Address");

getContentPane().add(jLabel6);

jLabel6.setBounds(5, 155, 150, 14);

jLabel5.setText("Student IC# ");

getContentPane().add(jLabel5);

jLabel5.setBounds(5, 130, 150, 14);

jLabel4.setText("Student Name");

getContentPane().add(jLabel4);

jLabel4.setBounds(5, 105, 150, 14);

jLabel2.setText("Student Code");

getContentPane().add(jLabel2);

jLabel2.setBounds(5, 80, 150, 14);

jLabel8.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));

getContentPane().add(jLabel8);

jLabel8.setBounds(130, 75, 160, 19);

jTextField2.setColumns(8);

jTextField2.setName("");

getContentPane().add(jTextField2);

jTextField2.setBounds(130, 100, 160, 19);

jTextField3.setColumns(8);

jTextField3.setName("");

getContentPane().add(jTextField3);

jTextField3.setBounds(130, 125, 160, 19);

jTextField4.setColumns(8);

jTextField4.setName("");

getContentPane().add(jTextField4);

jTextField4.setBounds(130, 150, 340, 19);

jTextField5.setColumns(10);

jTextField5.setName("");

getContentPane().add(jTextField5);

jTextField5.setBounds(130, 175, 160, 19);

jButton5.setText("F1-Retry");

jButton5.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

jButton5ActionPerformed(evt);

}

});

getContentPane().add(jButton5);

jButton5.setBounds(5, 34, 90, 23);

getContentPane().add(jSeparator2);

jSeparator2.setBounds(0, 30, 800, 2);

jLabel1.setText("Register Student");

getContentPane().add(jLabel1);

jLabel1.setBounds(5, 10, 150, 14);

getContentPane().add(jSeparator1);

jSeparator1.setBounds(0, 60, 800, 2);

jButton2.setText("F2-NxRec");

jButton2.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

jButton2ActionPerformed(evt);

}

});

getContentPane().add(jButton2);

jButton2.setBounds(100, 34, 90, 23);

jButton3.setText("F3-PreRec");

jButton3.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

jButton3ActionPerformed(evt);

}

});

getContentPane().add(jButton3);

jButton3.setBounds(195, 34, 90, 23);

jButton4.setText("F8-Save");

jButton4.addActionListener(new java.awt.event.ActionListener(){

publicvoid actionPerformed(java.awt.event.ActionEvent evt){

jButton4ActionPerformed(evt);

}

});

getContentPane().add(jButton4);

jButton4.setBounds(290, 34, 90, 23);

jLabel3.setText("mode : ADD");

jLabel3.setBorder(new javax.swing.border.TitledBorder(""));

getContentPane().add(jLabel3);

jLabel3.setBounds(680, 3, 100, 25);

setBounds(0, 0, 800, 600);

}

// </editor-fold>

privatevoid jButton5ActionPerformed(java.awt.event.ActionEvent evt){

{

......//some coding here.

}

privatevoid jButton4ActionPerformed(java.awt.event.ActionEvent evt){

......//some coding here.

}

privatevoid jButton3ActionPerformed(java.awt.event.ActionEvent evt){

......//some coding here.

}

privatevoid jButton2ActionPerformed(java.awt.event.ActionEvent evt){

......//some coding here.

}

/**

* @param args the command line arguments

*/

publicstaticvoid main(String args[]){

java.awt.EventQueue.invokeLater(new Runnable(){

publicvoid run(){

new RegisterStudent_ADD().setVisible(true);

}

});

}

// Variables declaration - do not modify

private javax.swing.JButton jButton2;

private javax.swing.JButton jButton3;

private javax.swing.JButton jButton4;

private javax.swing.JButton jButton5;

private javax.swing.JDialog jDialog1;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JLabel jLabel5;

private javax.swing.JLabel jLabel6;

private javax.swing.JLabel jLabel7;

public javax.swing.JLabel jLabel8;

private javax.swing.JOptionPane jOptionPane1;

private javax.swing.JSeparator jSeparator1;

private javax.swing.JSeparator jSeparator2;

private javax.swing.JTextField jTextField2;

private javax.swing.JTextField jTextField3;

private javax.swing.JTextField jTextField4;

private javax.swing.JTextField jTextField5;

// End of variables declaration

}

Ning.

[12104 byte] By [wei_ninga] at [2007-10-2 5:54:50]
# 1

Use the Action architecture (http://java.sun.com/products/jfc/tsc/articles/actions/):

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;

import javax.swing.Action;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.KeyStroke;

public class HotKeyComponent extends JPanel {

public static void main(String[] args) {

HotKeyComponent c = new HotKeyComponent();

UselessAction a1 = new UselessAction("Action 1", "action.1", KeyStroke.getKeyStroke("F1"));

UselessAction a2 = new UselessAction("Action 2", "action.2", KeyStroke.getKeyStroke("F2"));

UselessAction a3 = new UselessAction("Action 3", "action.3", KeyStroke.getKeyStroke("F4"));

c.addButton(a1);

c.addButton(a2);

c.addButton(a3);

JFrame frame = new JFrame("HotKey Test");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setBounds(100, 100, 400, 300);

frame.add(c);

frame.setVisible(true);

}

public void addButton(Action action) {

Object command = action.getValue(Action.ACTION_COMMAND_KEY);

KeyStroke stroke = (KeyStroke)action.getValue(Action.ACCELERATOR_KEY);

getInputMap(WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(stroke, command);

getActionMap().put(command, action);

JButton button = new JButton(action);

add(button);

}

static class UselessAction extends AbstractAction {

public UselessAction(String name, Object actionCommand, KeyStroke stroke) {

putValue(Action.NAME, name);

putValue(Action.ACTION_COMMAND_KEY, actionCommand);

putValue(Action.ACCELERATOR_KEY, stroke);

}

public void actionPerformed(ActionEvent actionEvent) {

System.out.println("actionPerformed " + getValue(Action.NAME));

}

}

}

The example contains three buttons. You can either click them, or press the key which is mapped to an Action.

joene19a at 2007-7-16 2:04:11 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...