Results 1 to 10 of 10
  1. #1
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default KeyEventListener without Alt key

    By using mnemonics property of a button we have to press the mnemonic key together with the Alt kay..
    is there a way that by pressing only one wanted key generate the same event?
    if yes then please explain with example code...
    Thank you

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by asifzbaig View Post
    By using mnemonics property of a button we have to press the mnemonic key together with the Alt kay..
    is there a way that by pressing only one wanted key generate the same event?
    if yes then please explain with example code...
    Thank you
    Yes, you can use key bindings, and you can find lots of sample code in the Oracle Java tutorials Swing section. Luck!

  3. #3
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    Hey it dosent seems of any use for me or you can say i am not so familiar to use it.
    Actually the problem is i have many buttons in my window and usualy key binding is used for all the buttons or other top components when THEY ARE FOCUSED.
    But i have to use keyboard buttons as shortcut for ACTION EVENTS...
    Could you please help me in doing so????

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by asifzbaig View Post
    Hey it dosent seems of any use for me or you can say i am not so familiar to use it.
    Actually the problem is i have many buttons in my window and usualy key binding is used for all the buttons or other top components when THEY ARE FOCUSED.
    But i have to use keyboard buttons as shortcut for ACTION EVENTS...
    Could you please help me in doing so????
    Perhaps you are thinking of KeyListeners which only work if the component has focus. Key Bindings can work even if the bound component doesn't have focus. Again the tutorial explains all and can be found here: How to Use Key Bindings (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)

  5. #5
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    Sorry dear i am unable to convert your idea and help into a correct code.

    Will you please explain me further with some example code???

    just take an example i have attached...

    In this code example i have applied actions to R, G and B named buttons to change the background colors

    Now please add the code with this to change the colors with r, g, and b keys of keyboard...

    Sorry for inconvinience........
    Attached Files Attached Files

  6. #6
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    Here is the same code :

    Java Code:
    import java.awt.Color;
    
    
    public class KeyEventDemo extends javax.swing.JPanel {
    
        
        public KeyEventDemo() {
            initComponents();
        }
    
    	    
        private javax.swing.JButton jButton1;
        private javax.swing.JButton jButton2;
        private javax.swing.JButton jButton3;
       
        @SuppressWarnings("unchecked")
        
        private void initComponents() {
    
            jButton1 = new javax.swing.JButton();
            jButton2 = new javax.swing.JButton();
            jButton3 = new javax.swing.JButton();
    
            jButton1.setText("R");
            jButton1.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton1ActionPerformed(evt);
                }
            });
    
            jButton2.setText("G");
            jButton2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton2ActionPerformed(evt);
                }
            });
    
            jButton3.setText("B");
            jButton3.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    jButton3ActionPerformed(evt);
                }
            });
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(74, 74, 74)
                    .addComponent(jButton1)
                    .addGap(5, 5, 5)
                    .addComponent(jButton2)
                    .addGap(5, 5, 5)
                    .addComponent(jButton3)
                    .addContainerGap(199, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(126, 126, 126)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButton1)
                        .addComponent(jButton2)
                        .addComponent(jButton3))
                    .addContainerGap(151, Short.MAX_VALUE))
            );
        }
    
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
            setBackground(Color.red);
        }
    
        private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
            setBackground(Color.green);
        }
    
        private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
            setBackground(Color.blue);
        }
    
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    javax.swing.JFrame frame = new javax.swing.JFrame("Key Event Demo");
                    frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
                    frame.getContentPane().add(new KeyEventDemo());
                    frame.pack();
                    frame.setVisible(true);
                
                }
            });
        }
    
        
    }
    Last edited by Fubarable; 05-13-2011 at 01:51 AM. Reason: code tags added

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by asifzbaig View Post
    Sorry dear i am unable to convert your idea and help into a correct code.

    Will you please explain me further with some example code???

    just take an example i have attached...

    In this code example i have applied actions to R, G and B named buttons to change the background colors

    Now please add the code with this to change the colors with r, g, and b keys of keyboard...

    Sorry for inconvinience........
    It's no inconvenience, but in my opinion since it is your project, the bulk of effort needs to come from you. I don't yet see your attempt at using key bindings, and I'm not about to try to re-write a tutorial that was written better than anything I can do. So to that end my recommendations are:

    1) Read or re-read the tutorial on key bindings.
    2) If you have any specific questions about the tutorial, ask them here
    3) Attempt to create a small solution using key bindings
    4) If it doesn't work, then post your small compilable sample program that doesn't work and your specific questions.

    Much luck!

  8. #8
    asifzbaig is offline Member
    Join Date
    May 2011
    Posts
    56
    Rep Power
    0

    Default

    I use this function to generate event in the example code:

    private void keyPressed(java.awt.event.KeyEvent evt) {
    int key=evt.getKeyCode();
    switch(key){
    case KeyEvent.VK_R:setBackground(Color.red);
    break;
    case KeyEvent.VK_G:setBackground(Color.green);
    break;
    case KeyEvent.VK_B:setBackground(Color.blue);
    break;
    }
    }

    But in my application how can i call the actionPerformed(Action e) methods in switch cases??

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by asifzbaig View Post
    But in my application how can i call the actionPerformed(Action e) methods in switch cases??
    For key binding you would use an AbstractAction for both the button and the key binding. Again, please read the tutorial.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One way to do this that has worked for me is to create a method that creates and returns a JButton with the key bindings attached to the JButton. To do this you would (in semi-pseudocode):

    Java Code:
       private JButton createButtonWithBinding(final JComponent jcomp, String buttonText, int keyCode, final Color color) {
          
          // create an abstract action that does what you want when button and key pressed
          
          // create JButton with the AbstractAction
          
          // create input and action maps from the JButton using 
          // using JComponent.WHEN_IN_FOCUSED_WINDOW; as the condition for the input ma
          
          // use maps to create key bindings
    
          // return the JButton
       }

Posting Permissions

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