Results 1 to 20 of 36
Thread: Ctrl+X on JButton
- 06-13-2010, 09:57 AM #1
- 06-13-2010, 10:14 AM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Find out what mnemonics and accelerators are. I suggest googling it.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 06-13-2010, 11:22 AM #3
I googled it but couldn't find anything that I want.....
Please can anybody help me.
- 06-13-2010, 12:00 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
I typed in java mnemonics in google. Lo and behold, 1st hit:
How to Use Actions (The Java™ Tutorials > Creating a GUI With JFC ...
Learn to do your own research.Ever seen a dog chase its tail? Now that's an infinite loop.
- 06-13-2010, 03:48 PM #5
Thanks m00nchile, I really appreciate your help but I did my research before hand and I had also seen that tutorial... it implements mnemonics which add Alt+x kind of shorcuts but I want Ctrl+x ....
I also tried to implement keylisteners but they require the button to have focuswhen i press ctrl+x (whats the point of adding shortcut then)...
Can you please tell me how do i implement that?
I posted my query here after I did my research and couldn't find anything appropriate. I really appreciate your efforts... Thanks
-
Key binding might allow you to assign ctrl-x to some action. Please have a look at the tutorial on this. After reading the tutorial, I suggest that you give it a try, and then if it doesn't work, come on back with a small compilable program that demonstrates your attempt at solving it this way. This way we can see what you're doing right, what you're doing wrong, and you will have done the bulk of the work towards solving your problem, which is right and proper.
Much luck.
- 06-13-2010, 04:36 PM #7
Thanks for the suggestion.Key binding might allow you to assign ctrl-x to some action. Please have a look at the tutorial on this. After reading the tutorial, I suggest that you give it a try, and then if it doesn't work, come on back with a small compilable program that demonstrates your attempt at solving it this way. This way we can see what you're doing right, what you're doing wrong, and you will have done the bulk of the work towards solving your problem, which is right and proper.
I tried that and got the same problem that the button needs to have the focus for the shortcut to work.
I attached my program for you to see. Please have a look.
Thanks :)
- 06-13-2010, 04:54 PM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
There are three different InputMaps. You are using the wrong one.the button needs to have the focus for the shortcut to work.
However, the normal solution for this is to create a "Exit" Action. Then this action can be shared by buttons and menu items.
Then you can add an "accelerator" to the menu item and it will create the Key Bindings for you. The Swing tutorial on "How to Use Menus" will have an example.
- 06-13-2010, 04:58 PM #9
There are only two methods to get input maps :There are three different InputMaps. You are using the wrong one.
javax.swing.JComponent.getInputMap()
javax.swing.JComponent.getInputMap(int)
I tries both of them. They both require the button to have the focus.
And I don't want to add any Menus on my program. Is there a way to achieve this without using menus???However, the normal solution for this is to create a "Exit" Action. Then this action can be shared by buttons and menu items.
Then you can add an "accelerator" to the menu item and it will create the Key Bindings for you. The Swing tutorial on "How to Use Menus" will have an example.
Thanks
- 06-13-2010, 09:49 PM #10
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Yes, use Key Bindings.Is there a way to achieve this without using menus???
If you don't understand how to use the methods from reading the API, then you can also read the section from the Swing tutoriial on "How to Use Key Bindings" for a more complete explanation.There are only two methods to get input maps ... They both require the button to have the focus.
- 06-14-2010, 04:51 AM #11
camickr, I think you should read the previous replies before posting a reply. It's mentioned that I have tried KeyBindings and I have also read the tutorial.
The problem is that for the shortcut to work, the button should receive focus.
And I want the shortcut to work irrespective of which control on the frame has focus.
- 06-14-2010, 05:01 AM #12
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I have read the replies and I have looked at your code.I think you should read the previous replies before posting a reply
And I told you there are 3 different InputMapsIt's mentioned that I have tried KeyBindings
Then you should understand how each of the 3 different InputMaps workI have also read the tutorial.
That is not true. The problem is you are using the wrong InputMap.The problem is that for the shortcut to work, the button should receive focus.
- 06-14-2010, 09:19 AM #13
Then please tell me the right InputMap if I am wrong. If I had known the right one why would I had created the thread in the first place.That is not true. The problem is you are using the wrong InputMap.
If you can, please provide a snippet for the right inputmap so that the shortcut works with a global focus.
Thanks
-
- 06-14-2010, 09:37 PM #15
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 06-15-2010, 04:33 AM #16
Do you even remember what are the three input maps? I could find only one type of InputMap class with only one constructor.I don't remember right off hand which is best right for your situation, but there are only three of them. The quickest way to solve this is to try one and if it doesn't work, try the other ones.
And, if you are referring the different InputMaps by the get methods then there are only two getInputMap() methods :
javax.swing.JComponent.getInputMap()
javax.swing.JComponent.getInputMap(int)
And I tried both of them but my problem is not solved.
I made a thread because I thought someone could help me here but all you could do was to tell me to do what I have already done.
If anyone else gets a solution to this problem, then please post it here.
- 06-15-2010, 04:44 AM #17
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Shoot man, the tutorials tell you directly how to get the different intput maps. You use the second constructor, the one that takes an int, and pass appropriate constants, and again the tutorials tell you which constants to use. I'll give you a hint. One of them is JComponent.WHEN_FOCUSED. As for sample code, that's all there for you to read and study too, much better than anything we can create. Me thinks you might need to try a bit harder if you are going to succeed in this field. Luck man.
- 06-15-2010, 04:53 AM #18
Please refer to the Java Documentation .... Java 2 Platform SE v1.3.1: Class InputMap .......... There is ONLY ONE constructor for InputMap class.Shoot man, the tutorials tell you directly how to get the different intput maps. You use the second constructor, the one that takes an int, and pass appropriate constants, and again the tutorials tell you which constants to use. I'll give you a hint. One of them is JComponent.WHEN_FOCUSED. How could you miss this? Me thinks you need to try a bit harder if you are going to succeed in this field. Luck man.
The one that takes an int as parameter is the getInputMap() method which I have mentioned in the previous reply and I have also mentioned that I have tried that also with the all the conditions namely,
JComponent.UNDEFINED_CONDITION
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
JComponent.WHEN_FOCUSED
JComponent.WHEN_IN_FOCUSED_WINDOW
I have done my part of the research and I am still looking for the answer because your answers are getting ambiguous even with the Java Documentation, let alone examples from some sites. I don't know which tutorials you refer to.
-
you are correct regarding constructor. 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.
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.
-
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