Results 1 to 10 of 10
  1. #1
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default 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.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Java Code:
     if(key == 192)
    Don't ever use code like that. I have no idea what 192 stands for.

    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.

  3. #3
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    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.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    If you don't know what an Action is then read the section from the Swing tutorial on How to Use Actions

    Am I to place "Ctrl Shift A" in the string object 'keyStrokeAndKey'?
    Look at the KeyStroke.getKeyStroke(String s) API description. I don't remember what is valid.
    Last edited by camickr; 05-03-2010 at 06:15 AM.

  5. #5
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    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.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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.

  7. #7
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Exclamation

    Here's the code.
    But I'm afraid there is an error on the line

    Java Code:
    application.getInputMap(...).put(keyStroke, keyStrokeAndKey);
    application.getActionMap().put(keyStrokeAndKey, action);
    What this program will do it when you press the combination control Z, the program ends.

    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 04:13 AM.
    Every project, package, class, method, variable, syntax, algorithm, etc.
    are registered in my memory bank. Thanks to this thread.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    "..." 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.

  9. #9
    chyrl is offline Senior Member
    Join Date
    Mar 2010
    Location
    Manila, Philippines
    Posts
    257
    Rep Power
    5

    Default

    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.

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    What I only ask is a straight guide to solve my thread clearly enough.
    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.

    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.

    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?
    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?

    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 05:39 PM.

Similar Threads

  1. Key combination
    By dejos456 in forum New To Java
    Replies: 9
    Last Post: 11-30-2009, 08:11 AM
  2. COMBINATION TEXT..help me..urgent
    By conv_bay in forum New To Java
    Replies: 10
    Last Post: 04-28-2009, 12:34 AM
  3. Image with JWindow -> trails
    By ofir3dvb in forum AWT / Swing
    Replies: 2
    Last Post: 03-18-2008, 06:21 PM
  4. JWindow/JFrame with boarders
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 12:35 PM
  5. How to mouse-drag a JWindow?
    By cruxblack in forum New To Java
    Replies: 3
    Last Post: 08-06-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •