Results 21 to 36 of 36
Thread: Ctrl+X on JButton
- 06-15-2010, 05:16 AM #21
Thank you for putting some efforts from your side now.you are correct regarding constructor.
And I have mentioned that I have tired both the overloaded methods with all the possible values for int.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.
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.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.
-
- 06-15-2010, 05:22 AM #23
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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.
- 06-15-2010, 05:43 AM #24
No,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?
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......
When I stated that i tried all the overloaded methods and all its combinations, it should have been cleared then itself.Finally you've actually stated that you tried "JComponent.WHEN_IN_FOCUSED_WINDOW".
Maybe, you are right here. I cannot test this.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.
I tabbed to calculate and VOILA !!! --- It DIDN'T work ---Tab to the Calculate button and the Control X will work.
It only works when the button on which the inputmap is configured has the focus.
Thanks for the suggestion. I tried that (CTRL+D and CTRL+J) also and again the same problem.So the solution to your problem is to choose another Key Binding to use.
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.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.
-
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); }
- 06-15-2010, 05:56 AM #26
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 :( -----
- 06-15-2010, 06:58 AM #27
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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.
OR add keylistener like thisJava 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); } } }
thenJava Code:public frmMaterials(){ JTextField1.addKeyListener(this); JTextField2.addKeyListener(this); JTextField3.addKeyListener(this); JButton1.addKeyListener(this); }
Hope this help.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){}
Goodluck
gejeLast edited by mine0926; 06-15-2010 at 07:07 AM.
- 06-15-2010, 09:15 AM #28
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 :)
- 06-16-2010, 02:23 AM #29
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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.
This may also help you.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); } }
http://java.sun.com/docs/books/tutor...sc/action.html
http://java.sun.com/docs/books/tutor...eybinding.html
GoodLuckLast edited by mine0926; 06-16-2010 at 02:25 AM.
- 06-16-2010, 04:19 AM #30
Thanks mine0926.... I will try it.
- 06-16-2010, 06:34 AM #31
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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.When I stated that i tried all the overloaded methods and all its combinations, it should have been cleared then itself.
Yes it does work, if you use the proper InputMap. I even tested it using your code (after I fixed the compile errors).I tabbed to calculate and VOILA !!! --- It DIDN'T work ---
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!
- 06-16-2010, 07:54 AM #32
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
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
- 06-16-2010, 09:20 AM #33
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
- 06-16-2010, 09:30 AM #34
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Well this
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).
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.
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".
- 06-16-2010, 09:55 AM #35
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
- 06-16-2010, 04:20 PM #36
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
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:I just wanna ask why KeyListener is not the correct approach?
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
Similar Threads
-
Check if ctrl is pressed in an ActionEvent
By poet in forum AWT / SwingReplies: 2Last Post: 09-10-2009, 08:50 AM -
Write ctrl-z characters
By jithan in forum New To JavaReplies: 3Last Post: 02-18-2009, 03:11 AM -
Classpath is broken after Ctrl+S
By vetalok in forum EclipseReplies: 2Last Post: 08-18-2008, 10:35 AM -
Run method on Ctrl+Shift+D
By Echilon in forum AWT / SwingReplies: 1Last Post: 02-16-2008, 10:31 PM -
How to add hotkey(Ctrl+VK) to a button?
By rpwtdj in forum Advanced JavaReplies: 5Last Post: 01-10-2008, 02:13 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks