Page 2 of 2 FirstFirst 12
Results 21 to 36 of 36
  1. #21
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    you are correct regarding constructor.
    Thank you for putting some efforts from your side now.

    But we get the specific InputMap for a given JComponent by calling getInputMap on the component. This method had two overloads, one taking an int parameter than can accept one of 3 values,
    JComponent.WHEN_FOCUSED
    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
    JComponent.WHEN_IN_FOCUSED_WINDOW

    and a default method taking no parameter but actually acting as if it were the prior method and that it took the JComponent.WHEN_FOCUSED parameter.
    And I have mentioned that I have tired both the overloaded methods with all the possible values for int.

    Those are your three choices, one works well when the component is in a window that holds a focused component, and thus JComponent.WHEN_IN_FOCUSED_WINDOW is likely your best parameter.

    Try it and find out.
    But here, you are wrong pal..... it's not "when the component is in a window that holds a focused component" .... it's " when the component is in the window that has the focus" and so it again creates the same problem that the window must have the focus not its components so the shortcut will only work when the window itself has focus ( and I am not writing that without trying it ). And what I need is that the shortcut should work when the focus is on ANY control within that frame.

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

    Default

    Quote Originally Posted by tyroceur View Post
    But here, you are wrong pal..... it's not "when the component is in a window that holds a focused component" .... it's " when the component is in the window that has the focus" and so it again creates the same problem that the window must have the focus not its components so the shortcut will only work when the window itself has focus ( and I am not writing that without trying it ). And what I need is that the shortcut should work when the focus is on ANY control within that frame.
    So you are trying to have a short cut key associated with a component that is not in the window of focus, is that what you are stating here?

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

    Default

    Finally you've actually stated that you tried "JComponent.WHEN_IN_FOCUSED_WINDOW".

    The Key Bindings do work. The problem is that when focus is on a "text field", it also has a Key Binding for "Control X", which is to do a "cut" of any selected text. The Key Binding for the component with focus takes precedence over the other general bindings.

    Tab to the Calculate button and the Control X will work.

    So the solution to your problem is to choose another Key Binding to use.

    I can't find any example of a Windows program that actually sets an accelerator to close an application. By default you can just close the window using Alt-F4 if you want an accelerator.

  4. #24
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    So you are trying to have a short cut key associated with a component that is not in the window of focus, is that what you are stating here?
    No,
    Let me explain ........
    lets say we have got 3 textboxes and 2 button on a jframe. Now, i want to add a shortcut to one of the buttons such that it should work when the focus is on either of the 3 textboxes or the 2 buttons or even the frame itself.
    I think now you will understand what i want......


    Finally you've actually stated that you tried "JComponent.WHEN_IN_FOCUSED_WINDOW".
    When I stated that i tried all the overloaded methods and all its combinations, it should have been cleared then itself.

    The Key Bindings do work. The problem is that when focus is on a "text field", it also has a Key Binding for "Control X", which is to do a "cut" of any selected text. The Key Binding for the component with focus takes precedence over the other general bindings.
    Maybe, you are right here. I cannot test this.

    Tab to the Calculate button and the Control X will work.
    I tabbed to calculate and VOILA !!! --- It DIDN'T work ---
    It only works when the button on which the inputmap is configured has the focus.

    So the solution to your problem is to choose another Key Binding to use.
    Thanks for the suggestion. I tried that (CTRL+D and CTRL+J) also and again the same problem.

    I can't find any example of a Windows program that actually sets an accelerator to close an application. By default you can just close the window using Alt-F4 if you want an accelerator.
    I don't want to specifically apply that to EXIT button. I can apply it to Calculate button if it works or any button in the future once I get it to work.

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

    Default

    Quote Originally Posted by tyroceur View Post
    Maybe, you are right here. I cannot test this.
    but it does:
    Java Code:
          JTextField[] fields = {lengthField, widthField, depthField, volumeField};
          bCalc = new javax.swing.JButton();
          bExit = new javax.swing.JButton();
          
          //!!
          for (JTextField textField : fields) {
             textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK), "NonAction");
             textField.getActionMap().put("NonAction", null);
          }

  6. #26
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    but it does:
    Java Code:
          JTextField[] fields = {lengthField, widthField, depthField, volumeField};
          bCalc = new javax.swing.JButton();
          bExit = new javax.swing.JButton();
          
          //!!
          for (JTextField textField : fields) {
             textField.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_X, Event.CTRL_MASK), "NonAction");
             textField.getActionMap().put("NonAction", null);
          }
    I replied that in context of my program that I cannot test this when InputMap is configured with a button and we are trying the shortcut with the focus on the textfield. It won't work for any shortcut ... that's why I wrote that. But you are also correct with your snippet. Thanks.

    ---- And I am still looking for an answer to my original question :( -----

  7. #27
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Can you show how you addkeylistener to your JComponents? Is it only the JButton that you add keylistener?


    try this:

    first Implement key listener
    Java Code:
    public class myForm extends javax.swing.JFrame [b]implements KeyListener[/b]{

    then add keylistener to your components. For me this is the easiest way to add listener to evry components that I want.
    Java Code:
        public frmMaterials(){
            int comCount = JPanel1.getComponentCount();
            for(int i = 0; i <= comCount-1; i++)
            {   if(JPanel1.getComponent(i) instanceof  JLabel){}
                else
                {   JPanel1.getComponent(i).addKeyListener(this);
                }
            }
     }
    OR add keylistener like this
    Java Code:
        public frmMaterials(){
            JTextField1.addKeyListener(this);
            JTextField2.addKeyListener(this);
            JTextField3.addKeyListener(this);
            JButton1.addKeyListener(this);
     }
    then
    Java Code:
        public void keyTyped(KeyEvent ek){}
    
        public void keyPressed(KeyEvent ek)
        {  if(ek.isControlDown() == true)   //If control key is press
           {   if(ek.getKeyCode() == KeyEvent.VK_A){JTextField1.requestFocus();}
               if(ek.getKeyCode() == KeyEvent.VK_B){JTextField2.requestFocus();}
               if(ek.getKeyCode() == KeyEvent.VK_B){JTextField3.requestFocus();}
               if(ek.getKeyCode() == KeyEvent.VK_G){JButton1.doClick();}
           }
        }
    
        public void keyReleased(KeyEvent ek){}
    Hope this help.

    Goodluck
    geje
    Last edited by mine0926; 06-15-2010 at 08:07 AM.

  8. #28
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    Yes, you are right mine0926. That's a way of having a global focus for the button to add keylisteners to every control on the form. Thanks.
    But won't this method be an alternative approach to solve this problem. Do you know a way in Java by which we can accomplish this without bothering the other controls on the frame like we do when we add accelerators on menu items.
    Thanks for your help. I really appreciate it :)

  9. #29
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I am not sure but I think NO. For every event you want to catch you should put listener to that component.

    You might want to visit this site: http://www.gamedev.net/community/for...opic_id=542306

    But still the sample uses addActionListener. This code is same with the code in the link.
    Java Code:
            //Create an action for buttons1.
            //I could have created one action for both buttons and used e.getSource() to find which button was the source of the event.
            Action button1Action = new AbstractAction(){
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "You activated button1");
                }
            };
    
            //Set up the mappings so that key events get mapped to actions
            //The string can actually be any object that is different for each action. I just used these strings for ease since "b1a" is different than "b2a".
            button1.getInputMap(JOptionPane.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.CTRL_DOWN_MASK), "b1a");
            button1.getActionMap().put("b1a", button1Action);
    
            //An action is a subclass of ActionListener, so we can do the following:
            [b]button1.addActionListener(button1Action);[/b]
    
            //Add the button to the panel
            panel.add(button1);
    
            //Set up button2
            JButton button2 = new JButton("Button2 (ctrl+2)");
    
            Action button2Action = new AbstractAction(){
                public void actionPerformed(ActionEvent e) {
                    JOptionPane.showMessageDialog(null, "You activated button2");
                }
            };
    
            button2.getInputMap(JOptionPane.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_2, KeyEvent.CTRL_DOWN_MASK), "b2a");
            button2.getActionMap().put("b2a", button2Action);
            [b]button2.addActionListener(button2Action);[/b]
            panel.add(button2);
        }
    }
    This may also help you.
    http://java.sun.com/docs/books/tutor...sc/action.html
    http://java.sun.com/docs/books/tutor...eybinding.html

    GoodLuck
    Last edited by mine0926; 06-16-2010 at 03:25 AM.

  10. #30
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    Thanks mine0926.... I will try it.

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

    Default

    When I stated that i tried all the overloaded methods and all its combinations, it should have been cleared then itself.
    No it wasn't clear which is why 3 people have told you to use the proper InputMap and even posted pieces of the documentation from the API to prove to you that multiple InputMaps exist.

    I tabbed to calculate and VOILA !!! --- It DIDN'T work ---
    Yes it does work, if you use the proper InputMap. I even tested it using your code (after I fixed the compile errors).

    Using a KeyListener is not the correct approach. Key Binding is the proper approach and you've been given the solution.

    Post your SSCCE that proves that my suggestion doesn't work!

  12. #32
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I just wanna ask why KeyListener is not the correct approach? I use KeyListener to set shortcut keys in my buttons and textfields.

    Thanks,
    geje

  13. #33
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    No it wasn't clear which is why 3 people have told you to use the proper InputMap and even posted pieces of the documentation from the API to prove to you that multiple InputMaps exist.



    Yes it does work, if you use the proper InputMap. I even tested it using your code (after I fixed the compile errors).

    Using a KeyListener is not the correct approach. Key Binding is the proper approach and you've been given the solution.

    Post your SSCCE that proves that my suggestion doesn't work!
    Wow, its pity that simple plain english isn't clear to you. Look at the replies and tell me when have I denied the existence of diffrent InputMaps.

    Yes, I have tried all the combinations of InputMaps and I don't have time to rewrite the code that doesn't work, just to prove it to you that it doesn't work.
    But don't worry I will explain why it didn't worked... for each value :
    --> WHEN_ANCESTOR_OF_FOCUSED_COMPONENT means that the command should be invoked when the receiving component is an ancestor of the focused component or is itself the focused component. ------- this will work ONLY when the button on which inputMap is assigned has focus because that button is not an ancestor of any component on the frame.

    --> WHEN_FOCUSED means that the command should be invoked when the component has the focus. ------ It clearly states that it will work ONLY when the button have the focus.

    --> WHEN_IN_FOCUSED_WINDOW means that the command should be invoked when the receiving component is in the window that has the focus or is itself the focused component. ----- it will also work ONLY when the receiving component that is the button itself has focus because no matter how many times you press TAB you cannot have the window to have the focus.

    And....you say it works for 'proper' InputMap. Then why don't you tell me the proper InputMap. Or better still, you Post your SSCCE that proves that your code works.

    Fair Enough

  14. #34
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by tyroceur View Post
    Wow, its pity that simple plain english isn't clear to you. Look at the replies and tell me when have I denied the existence of diffrent InputMaps.
    Well this
    Quote Originally Posted by tyroceur View Post
    There are only two methods to get input maps :

    javax.swing.JComponent.getInputMap()
    javax.swing.JComponent.getInputMap(int)

    I tries both of them. They both require the button to have the focus
    Sure as hell at least looks like you expect a third method for a third inputMap and that each of those will only return one possible inputMap (i.e. the other two).

    Quote Originally Posted by tyroceur View Post
    Yes, I have tried all the combinations of InputMaps and I don't have time to rewrite the code that doesn't work, just to prove it to you that it doesn't work.
    But don't worry I will explain why it didn't worked... for each value :
    --> WHEN_ANCESTOR_OF_FOCUSED_COMPONENT means that the command should be invoked when the receiving component is an ancestor of the focused component or is itself the focused component. ------- this will work ONLY when the button on which inputMap is assigned has focus because that button is not an ancestor of any component on the frame.

    --> WHEN_FOCUSED means that the command should be invoked when the component has the focus. ------ It clearly states that it will work ONLY when the button have the focus.

    --> WHEN_IN_FOCUSED_WINDOW means that the command should be invoked when the receiving component is in the window that has the focus or is itself the focused component. ----- it will also work ONLY when the receiving component that is the button itself has focus because no matter how many times you press TAB you cannot have the window to have the focus.
    You were asked to post your code for this one, as, obviously, when a component in the window has focus, then the window has focus as well. You just have to make sure that a component that already has a "shortcut" for that binding is not the component that has the focus.

    Quote Originally Posted by tyroceur View Post
    And....you say it works for 'proper' InputMap. Then why don't you tell me the proper InputMap. Or better still, you Post your SSCCE that proves that your code works.

    Fair Enough
    Yes he did, since he mentioned WHEN_IN_FOCUSED_WINDOW explicitly, in another post and quoted the "result" from your "attempt" in that post. And why should he post his? Are you simply looking for a cut-n-paste solution. The whole purpose here is for you to solve your problem, with hints and nudges from others, so that you actually learn something and can become more than a "code monkey".

  15. #35
    tyroceur's Avatar
    tyroceur is offline Member
    Join Date
    May 2010
    Location
    Fast Growing City, INDIA
    Posts
    44
    Rep Power
    0

    Default

    One thing I will say is that I never look for a cut-n-paste solution. I just wanted a start to the problem which I think I have got now.

    Thanks for all you help

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

    Default

    I just wanna ask why KeyListener is not the correct approach?
    This was the old approach used by AWT before Swing existed. KeyEvents are only dispatched to the componenent with focus which is the main problem of this question. The OP want to invoke the Action no matter what component has focus. All Swing components now use Key Bindings (read the tutorial link given above) which can solve this problem since you can control which InputMap you want to bind the the KeyStroke to. This easily solves the OP's problem since it will handle invoking the Action no matter what component has focus (again as I mentioned earlier assuming the component that has focus does not handle that KeyStroke first). The solution to the OP's problem is:

    a) a change to one line of code to make sure the proper InputMap is used
    b) a change to the KeyStroke to make sure no other components use that KeyStroke

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Check if ctrl is pressed in an ActionEvent
    By poet in forum AWT / Swing
    Replies: 2
    Last Post: 09-10-2009, 09:50 AM
  2. Write ctrl-z characters
    By jithan in forum New To Java
    Replies: 3
    Last Post: 02-18-2009, 04:11 AM
  3. Classpath is broken after Ctrl+S
    By vetalok in forum Eclipse
    Replies: 2
    Last Post: 08-18-2008, 11:35 AM
  4. Run method on Ctrl+Shift+D
    By Echilon in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2008, 11:31 PM
  5. How to add hotkey(Ctrl+VK) to a button?
    By rpwtdj in forum Advanced Java
    Replies: 5
    Last Post: 01-10-2008, 03:13 AM

Tags for this Thread

Posting Permissions

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