Results 1 to 4 of 4
  1. #1
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default [SOLVED] Key binding Q

    Hello,

    In a test app I bind(InputMap and ActionMap) the "UP,DOWN,RIGHT, and LEFT" arrow-keys to the main frame and set them to move the window(frame), so far it's working right, but if a TextField got focus, then it will consume the LEFT & RIGHT keys, and so the main window would not react (in this case move) to those keys.

    Obviously this is the expected behaviour, but just for test, can I overright it to do the opposite? and/or allow both actions?

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

    Default

    You can give the textfield's a null action: a string in the inputmap that does not correlate with one in the actionmap, and that may allow your mappings to work.

    caveat: this has not been tested by me today but is from my crusty memory, so check it out first before assuming it's gospel!

  3. #3
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You can give the textfield's a null action: a string in the inputmap that does not correlate with one in the actionmap, and that may allow your mappings to work.
    Hello thanks, :)

    Yes this seems do the overrighting.... :cool:
    Instead of the null act, I specify the same action...
    Java Code:
    javax.swing.JRootPane rp = this.getRootPane();
            InputMap imap = rp.getInputMap(
                    javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW);
            ActionMap amap = rp.getActionMap();
            
            imap.put(KeyStroke.getKeyStroke("LEFT"), "LEFT");
            amap.put("LEFT", new ChangeLoc(3));
            
            imap.put(KeyStroke.getKeyStroke("RIGHT"), "RIGHT");
            amap.put("RIGHT", new ChangeLoc(4));
            
            InputMap imap2 = txtDummy.getInputMap();
            ActionMap amap2 = txtDummy.getActionMap();
            
            imap2.put(KeyStroke.getKeyStroke("LEFT"), "LEFT");
            amap2.put("LEFT", new ChangeLoc(3));
            
            imap2.put(KeyStroke.getKeyStroke("RIGHT"), "RIGHT");
            amap2.put("RIGHT", new ChangeLoc(4));
    However the thing I didn't like about it is that, if there were 10 textfields then do i have to create 10 map for them?
    Still need to figure out how to allow both actions.... i.e. window(frame) moves and TextField's caret position changes....

  4. #4
    playwin2 is offline Member
    Join Date
    Aug 2008
    Posts
    58
    Rep Power
    0

    Default

    Hello,

    Found KeyEventDispatcher, it seems it can do what I'm looking for. :D

    For just moving the window, redispach the event to the frame, and for both actions clone the event and then redispatch. working smoothly. :D
    Last edited by playwin2; 11-04-2008 at 06:08 AM.

Similar Threads

  1. Replies: 1
    Last Post: 05-08-2008, 09:38 AM
  2. Spring binding in jsp
    By Pierre Javason in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-05-2008, 12:21 PM
  3. Dynamic Binding
    By javarishi in forum New To Java
    Replies: 3
    Last Post: 04-09-2008, 11:17 AM

Posting Permissions

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