Results 1 to 10 of 10
- 05-02-2010, 03:21 PM #1
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
JWindow closes after a particular key combination
Hi again.
I can't seem to find on how to get my key combination functioning well.
The key combination I want to happen is " (Left)Ctrl + (Left)Shift + 'a' "
Here's my code for you to better solve this thread.
Thanks.
Java Code:import java.awt.Dimension; import java.awt.Window; import java.awt.Toolkit; import javax.swing.JOptionPane; import javax.swing.JWindow; import javax.swing.JFrame; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; public class Application { private JFrame frame = new JFrame("Test"); private Window window = new Window(frame); private Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize(); private JWindow application = new JWindow(window); public Application() { application.addKeyListener(new keyListener()); application.setAlwaysOnTop(true); application.setSize(resolution); application.setVisible(true); } private class keyListener implements KeyListener { public void keyPressed(KeyEvent event) { int key = event.getModifiersEx(); System.out.println(key); if(key == 192) { int option = JOptionPane.showConfirmDialog(null, "Do you wish to exit?", "Confirmation", JOptionPane.YES_NO_OPTION); if(option==0) System.exit(0); } } public void keyReleased(KeyEvent event) { } public void keyTyped(KeyEvent e) { } } public static void main(String[] args){ new Application(); } }Every project, package, class, method, variable, syntax, algorithm, etc.
are registered in my memory bank. Thanks to this thread.
- 05-02-2010, 05:06 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Don't ever use code like that. I have no idea what 192 stands for.Java Code:if(key == 192)
Also, don't use KeyListeners for this. Swing was designed to use bind Actions to KeyStrokes. Read my write up on Key Bindings which will show you the current bindings that exist for every component. It will also show you how to add your own.
- 05-03-2010, 05:05 AM #3
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
camickr:
The link you've given me is useful enough yet I understand its logic.
Especially on the Action action = new AbstractAction() { ... }; part.
What is to be placed there?
Am I to place "Ctrl Shift A" in the string object 'keyStrokeAndKey'?Every project, package, class, method, variable, syntax, algorithm, etc.
are registered in my memory bank. Thanks to this thread.
- 05-03-2010, 05:10 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
If you don't know what an Action is then read the section from the Swing tutorial on How to Use Actions
Look at the KeyStroke.getKeyStroke(String s) API description. I don't remember what is valid.Am I to place "Ctrl Shift A" in the string object 'keyStrokeAndKey'?Last edited by camickr; 05-03-2010 at 05:15 AM.
- 05-04-2010, 03:52 AM #5
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
I can't get the code tutorial given in your link working.
What is the object required in the line, component.getInputMap(...).put(keyStroke, keyStrokeAndKey); ?
The component is a JFrame and a JWindow.Every project, package, class, method, variable, syntax, algorithm, etc.
are registered in my memory bank. Thanks to this thread.
- 05-05-2010, 01:04 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
The blog entry gives specific information for how you do a binding on a JFrame or JDialog.
If you need more help post your SSCCE.
- 05-05-2010, 03:11 AM #7
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
Here's the code.
But I'm afraid there is an error on the line
What this program will do it when you press the combination control Z, the program ends.Java Code:application.getInputMap(...).put(keyStroke, keyStrokeAndKey); application.getActionMap().put(keyStrokeAndKey, action);
Java Code:import java.awt.Dimension; import java.awt.Window; import java.awt.Toolkit; import java.awt.event.ActionEvent; import javax.swing.JWindow; import javax.swing.JFrame; import javax.swing.Action; import javax.swing.AbstractAction; import javax.swing.KeyStroke; public class Application { private JFrame frame = new JFrame("Test"); private Window window = new Window(frame); private Dimension resolution = Toolkit.getDefaultToolkit().getScreenSize(); private JWindow application = new JWindow(window); public Application() { Action action = new AbstractAction() { public void actionPerformed(ActionEvent e) { System.exit(0); }}; String keyStrokeAndKey = "control Z"; KeyStroke keyStroke = KeyStroke.getKeyStroke(keyStrokeAndKey); application.getInputMap(...).put(keyStroke, keyStrokeAndKey); application.getActionMap().put(keyStrokeAndKey, action); application.setAlwaysOnTop(true); application.setSize(resolution); application.setVisible(true); } public static void main(String[] args){ new Application(); } }Last edited by chyrl; 05-05-2010 at 03:13 AM.
Every project, package, class, method, variable, syntax, algorithm, etc.
are registered in my memory bank. Thanks to this thread.
- 05-05-2010, 04:54 AM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
"..." is not a valid parameter. The "..." is my way of telling you that you need to provide your own code, which means you have to read the API to find out what parameter to use.
The reason I create my blog is so I can provide suggestions to help solve problems. By creating the blog entries I don't have to repeat myself over and over and over every time I see a related question. Instead I can just provide a link and let you do the reading.
The blog points out that there is a difference between adding a binding to a component or a JFrame and JDialog. I specifically mentioned that in my previous reply!
If you don't find the blog helpfull, then you can always search the forurm or the web for examples. The keywords to use would be the name of the method that you don't understand.
Good luck.
- 05-05-2010, 01:45 PM #9
Senior Member
- Join Date
- Mar 2010
- Location
- Manila, Philippines
- Posts
- 257
- Rep Power
- 4
Your blog is useful enough to solve my problem.
Yet, I can't find a way to understood the two lines which makes my error wrong.
I have scanned the API. But I can't find the getInputMap() and getActionMap() in the any of the other component other than the JComponent itself. And how is that possible that the two components didn't inherit this two methods yet the JFrame and JWindow is a sub-class of JComponent?
I understood your reason of your blog. What I only ask is a straight guide to solve my thread clearly enough. That the reason how I found the answer on my previous thread regarding JTable update using the DefaultTableModel.Every project, package, class, method, variable, syntax, algorithm, etc.
are registered in my memory bank. Thanks to this thread.
- 05-05-2010, 04:33 PM #10
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
And I gave you an example in the blog. A JFrame, JDialog, JWindow is different. Your code does not look like my example given in the blog. This is my last response. You can't take the time to read the blog "carefully" and the reason I created the blog is so that I would not have to reply 10 times to every question. If you read it carefully you will find the problem.What I only ask is a straight guide to solve my thread clearly enough.
Also if you read the API for the getInputMap(...) method you will find the list of valid parameters for that method. You can't program if you can't read and I'm not about to do the reading for you. I've pointed you in the right direction.
Why do you think JFrame is a sub class of JComponent? Just because the class starts with a "J" does not make this true. Did you look at the API for JFrame to find all its parent classes?And how is that possible that the two components didn't inherit this two methods yet the JFrame and JWindow is a sub-class of JComponent?
You need to stop programming and learn how to use and read the API properly. It will save you time and effort in the future, to learn the basics first. Don't make assumptions.Last edited by camickr; 05-05-2010 at 04:39 PM.
Similar Threads
-
Key combination
By dejos456 in forum New To JavaReplies: 9Last Post: 11-30-2009, 07:11 AM -
COMBINATION TEXT..help me..urgent
By conv_bay in forum New To JavaReplies: 10Last Post: 04-27-2009, 11:34 PM -
Image with JWindow -> trails
By ofir3dvb in forum AWT / SwingReplies: 2Last Post: 03-18-2008, 05:21 PM -
JWindow/JFrame with boarders
By Java Tip in forum Java TipReplies: 0Last Post: 03-12-2008, 11:35 AM -
How to mouse-drag a JWindow?
By cruxblack in forum New To JavaReplies: 3Last Post: 08-06-2007, 09:52 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks