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

    Default Ctrl+X on JButton

    Can anybody tell me how do I add a shorcut key like Ctrl+x for a JButton in Java?

    Thanks :)

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Find out what mnemonics and accelerators are. I suggest googling it.
    Ever seen a dog chase its tail? Now that's an infinite loop.

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

    Default

    I googled it but couldn't find anything that I want.....
    Please can anybody help me.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

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

    Default

    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

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

    Default

    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.

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

    Default

    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.
    Thanks for the suggestion.
    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 :)
    Attached Files Attached Files

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

    Default

    the button needs to have the focus for the shortcut to work.
    There are three different InputMaps. You are using the wrong one.

    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.

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

    Default

    There are three different InputMaps. You are using the wrong one.
    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.


    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.
    And I don't want to add any Menus on my program. Is there a way to achieve this without using menus???

    Thanks

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

    Default

    Is there a way to achieve this without using menus???
    Yes, use Key Bindings.

    There are only two methods to get input maps ... They both require the button to have the focus.
    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.

  11. #11
    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
    Yes, use Key Bindings.



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

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

    Default

    I think you should read the previous replies before posting a reply
    I have read the replies and I have looked at your code.

    It's mentioned that I have tried KeyBindings
    And I told you there are 3 different InputMaps

    I have also read the tutorial.
    Then you should understand how each of the 3 different InputMaps work

    The problem is that for the shortcut to work, the button should receive focus.
    That is not true. The problem is you are using the wrong InputMap.

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

    Default

    That is not true. The problem is you are using the wrong InputMap.
    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.
    If you can, please provide a snippet for the right inputmap so that the shortcut works with a global focus.
    Thanks

  14. #14
    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 tyroceur View Post
    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.
    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. :rolleyes:

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,533
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Fubarable View Post
    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. :rolleyes:
    Duh, empirist ;-) Donald Knuth: "Note that I didn't test it, I only proved it to be correct".

    kind regards,

    Jos

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

    Default

    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.
    Do you even remember what are the three input maps? I could find only one type of InputMap class with only one constructor.

    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.

  17. #17
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by tyroceur View Post
    Do you even remember what are the three input maps? I could find only one type of InputMap class with only one constructor.

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

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

    Default

    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.
    Please refer to the Java Documentation .... Java 2 Platform SE v1.3.1: Class InputMap .......... There is ONLY ONE constructor for InputMap class.

    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.

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

    Default

    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.

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

Page 1 of 2 12 LastLast

Similar Threads

  1. Check if ctrl is pressed in an ActionEvent
    By poet in forum AWT / Swing
    Replies: 2
    Last Post: 09-10-2009, 08:50 AM
  2. Write ctrl-z characters
    By jithan in forum New To Java
    Replies: 3
    Last Post: 02-18-2009, 03:11 AM
  3. Classpath is broken after Ctrl+S
    By vetalok in forum Eclipse
    Replies: 2
    Last Post: 08-18-2008, 10:35 AM
  4. Run method on Ctrl+Shift+D
    By Echilon in forum AWT / Swing
    Replies: 1
    Last Post: 02-16-2008, 10: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, 02: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
  •