File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Help! About hotkey Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Reply Bookmark "Help! About hotkey" Watch "Help! About hotkey" New topic
Author

Help! About hotkey

kim jungil
Greenhorn

Joined: Sep 27, 2001
Posts: 27
I need help!!
Now, i want to make an application with several hotKeys in JFrame.My source like this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.util.*;
public class Demo extends JFrame {
public Demo () {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.enableEvents(AWTEvent.KEY_EVENT_MASK);
getContentPane().setLayout(null);
closeBtt.setBounds(new Rectangle(0, 13, 19, 40));
closeBtt.setBorder(new TitledBorder(""));
closeBtt.setHorizontalAlignment(SwingConstants.LEFT);
closeBtt.setHorizontalTextPosition(SwingConstants.LEFT);
closeBtt.setMargin(new Insets(0, 0, 0, 0));
closeBtt.setMnemonic('0');
closeBtt.setText("X");
closeBtt.setVerticalAlignment(SwingConstants.TOP);
closeBtt.setVerticalTextPosition(SwingConstants.TOP);
this.setEnabled(true);
templateLab.setText("template");
templateLab.setBounds(new Rectangle(23, 25, 80, 17));
templateCB.setBounds(new Rectangle(103, 23, 109, 21));
codeLab.setText("name");
codeLab.setBounds(new Rectangle(225, 26, 33, 17));
codeTxt.setBounds(new Rectangle(266, 23, 63, 21));
setBtt.setBounds(new Rectangle(340, 20, 79, 27));
setBtt.setBorder(new TitledBorder(""));
setBtt.setText("set");
getContentPane().add(templateCB, null);
getContentPane().add(codeTxt, null);
getContentPane().add(setBtt, null);
getContentPane().add(closeBtt, null);
getContentPane().add(templateLab, null);
this.getContentPane().add(codeLab, null);
}
protected void processKeyEvent(KeyEvent evt) {
super.processKeyEvent(evt);
if(evt.getKeyCode() == KeyEvent.VK_F4){
System.out.println("AAAA");
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
;
}
Demo demo = new Demo();
demo.setBounds(50,50,479, 80);
demo.show();
}
private JButton closeBtt = new JButton();
private JLabel templateLab = new JLabel();
private JComboBox templateCB = new JComboBox();
private JLabel codeLab = new JLabel();
private JTextField codeTxt = new JTextField();
private JButton setBtt = new JButton();
}
when i clicked the F4 button, i can not catch the KeyEvent. "AAAA" is not printed.
What problem with my program?
help me, plz
thank u

BTW, if i remove all the components from JFrame except label, "AAAA" will be printed.
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
Without trying it, I would say, you need to add a KeyListener to your JFrame. There you can listen for the "F4" key and do some action. There you have three functions (Events) keyPressed, keyTyped and keyReleased. Which of those functions you implement, this depends on what you want. Probably keyReleased is not bad.
kim jungil
Greenhorn

Joined: Sep 27, 2001
Posts: 27
Rene Liebmann, thank u for you reply.
but this it neednt add keyListener in any components, because i use processEvent not keyTyped or other method in KeyListener.
"protected void processEvent(KeyEvent evt)" is overrided the JFrame's method, it can catch the keyListener.Because i have added "this.enableEvents(AWTEvent.KEY_EVENT_MASK);".

u can complie my source and run it, if there is no components on JFrame, it can catch the KeyListener, but if i added JButton or JTextField it can not catch it.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

I am closing this topic... it is a copy of this one in this forum...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help! About hotkey
 
Similar Threads
Updating values in a jTable
breaking my dialog into 3 tabs
JCheckBox?
Help!!!!!How to add hotkeys into JFrame
Simple example of passing a variable between 2 forms