aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to create hot key? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to create hot key?" Watch "How to create hot key?" New topic
Author

How to create hot key?

wei ning
Greenhorn

Joined: Nov 23, 2005
Posts: 3
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 including How 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.*;

public class RegisterStudent_ADD extends 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 ">
private void 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() {
public void 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() {
public void 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() {
public void 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() {
public void 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>


private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {
{
......//some coding here.
}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
......//some coding here.
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
......//some coding here.
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
......//some coding here.
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void 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
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Problem with hot keys wth the function keys is that they will call your function and also do their orginal function. So if you hit f5 then it will refresh the page. Not good and you can not beat that.

Look at the third script on this page: http://www10.brinkster.com/A1ien51/Scripts/KeyCode.htm

Eric
Vicky Pandya
Ranch Hand

Joined: Dec 16, 2004
Posts: 148
you are talking about event handling in Java. You are in wrong forum.
Originally posted by wei ning:
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 including How 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
Ning
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61202
    
  66

Moved to the Swing forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to create hot key?