How to trigger an event by using
Hi all,
I have created a jbutton and an event for the jbutton, for example,
privatevoid jButton1ActionPerformed(java.awt.event.ActionEvent evt)
Now, I would like to trigger the event by pressing a hot key, for example "F1", how can i make it? Can anyone help me? Thanks.
Ning.
[374 byte] By [
wei_ninga] at [2007-10-2 5:25:27]

Hi,
I have read through the information that you provided and tried to integrate it into my coding, but I'm really not so clear about that and i don't know how to apply into my coding correctly......can anyone help me? Thanks.
I'm creating an application using the J2SE v 1.4.2_10 SDK with NetBeans 4.1 Bundle. By the way, below is a simple testing program that including component jLabel1, jButton1 and jButton2. Both of the button are hold an event each. I would like to create a hotkey for each button, "F1" key for jButtton1, "F2" key for jButton2. If I click on the "F1" key, the jLabel1 will displays the character "ABC", if I click on the "F2" key, the jLabel1 will display the character "123". Can anyone provide the code to me? Thanks.
/*
* test.java
*/
public class test extends javax.swing.JFrame {
/** Creates new form test */
public test() {
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 ">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
getContentPane().setLayout(null);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("ABC");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(90, 70, 90, 23);
jButton2.setText("123");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
getContentPane().add(jButton2);
jButton2.setBounds(191, 70, 80, 23);
jLabel1.setBorder(new javax.swing.border.BevelBorder(javax.swing.border.BevelBorder.LOWERED));
getContentPane().add(jLabel1);
jLabel1.setBounds(110, 134, 150, 20);
setBounds(20, 20, 400, 300);
}
// </editor-fold>
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jLabel1.setText("123");
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jLabel1.setText("ABC");
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new test().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
// End of variables declaration
}
Ning.
here's a very simple example of one way to do it
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Testing extends JFrame
{
public Testing()
{
setLocation(400,100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel jp = new JPanel();
JButton btn = new JButton("Click Me");
jp.add(btn);
getContentPane().add(jp);
pack();
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
showButtonMessage();}});
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(new KeyEventDispatcher(){
public boolean dispatchKeyEvent(KeyEvent e){
if(e.getID() == KeyEvent.KEY_PRESSED)
{
if(e.getKeyCode() == KeyEvent.VK_F1 ) showButtonMessage();
}
return false;}});
}
public void showButtonMessage()
{
JOptionPane.showMessageDialog(this,"you clicked the button");
}
public static void main (String[] args){new Testing().setVisible(true);}
}