Results 1 to 3 of 3
  1. #1
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default can one disable key bindings on a JComponent?

    Suppose that I've got a set of keys bound to a JComponent. Something like this:

    Java Code:
        public void bindKeys() {
    
    	// variable declarations:
    	InputMap inputMap   = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    	ActionMap actionMap = getActionMap();
    
    	int PRESSED  = InputProcessor.PRESSED;
    	int RELEASED = InputProcessor.RELEASED;
    
    	int UP       = KeyEvent.VK_UP;
    	int RIGHT    = KeyEvent.VK_RIGHT;
    	int DOWN     = KeyEvent.VK_DOWN;
    	int LEFT     = KeyEvent.VK_LEFT;
    
    	int N 	     = KeyEvent.VK_N; // new maze
    	int R	     = KeyEvent.VK_R; // restart level
    	int Q	     = KeyEvent.VK_Q; // quit
    	int P	     = KeyEvent.VK_P; // pause
    
    	// up arrow key
    	inputMap.put(KeyStroke.getKeyStroke(UP, 0, false), "up_pressed");
    	actionMap.put("up_pressed", new InputProcessor(PRESSED, UP));
    	inputMap.put(KeyStroke.getKeyStroke(UP, 0, true), "up_released");
    	actionMap.put("up_released", new InputProcessor(RELEASED, UP));	
    
    	// right arrow key
    	inputMap.put(KeyStroke.getKeyStroke(RIGHT, 0, false), "right_pressed");
    	actionMap.put("right_pressed", new InputProcessor(PRESSED, RIGHT));
    	inputMap.put(KeyStroke.getKeyStroke(RIGHT, 0, true), "right_released");
    	actionMap.put("right_released", new InputProcessor(RELEASED, RIGHT));
    
    	// down arrow key
    	inputMap.put(KeyStroke.getKeyStroke(DOWN, 0, false), "down_pressed");
    	actionMap.put("down_pressed", new InputProcessor(PRESSED, DOWN));
    	inputMap.put(KeyStroke.getKeyStroke(DOWN, 0, true), "down_released");
    	actionMap.put("down_released", new InputProcessor(RELEASED, DOWN));
    
    	// left arrow key
    	inputMap.put(KeyStroke.getKeyStroke(LEFT, 0, false), "left_pressed");
    	actionMap.put("left_pressed", new InputProcessor(PRESSED, LEFT));
    	inputMap.put(KeyStroke.getKeyStroke(LEFT, 0, true), "left_released");
    	actionMap.put("left_released", new InputProcessor(RELEASED, LEFT));
    
    	// 'n' (for new maze)
    	inputMap.put(KeyStroke.getKeyStroke(N, 0, false), "n_pressed");
    	actionMap.put("n_pressed", new InputProcessor(PRESSED, N));
    
    	// 'r' (for restart level)
    	inputMap.put(KeyStroke.getKeyStroke(R, 0, false), "r_pressed");
    	actionMap.put("r_pressed", new InputProcessor(PRESSED, R));
    
    	// 'q' (for quit)
    	inputMap.put(KeyStroke.getKeyStroke(Q, 0, false), "q_pressed");
    	actionMap.put("q_pressed", new InputProcessor(PRESSED, Q));
    
    	// 'p' (for pause)
    	inputMap.put(KeyStroke.getKeyStroke(P, 0, false), "p_pressed");
    	actionMap.put("p_pressed", new InputProcessor(PRESSED, P));
        }
    Is there a simple way to temporarily disable the key bindings? Something like

    Java Code:
    myComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).setEnabled(false);
    I know about the InputMap.remove(KeyStroke) but I'm wondering if there's something simpler (and something that doesn't involve actually removing the key bindings) like the hypothetical setEnabled(boolean) method above.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Get a reference to each Action from the ActionMap and call setEnabled(false) on it?

    If you need more help, I think you're going to have to explain the rationale behind the requirement. You might also consider posting a SSCCE.
    SSCCE : Java Glossary

    db

  3. #3
    gib65 is offline Member
    Join Date
    Jun 2010
    Posts
    86
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Get a reference to each Action from the ActionMap and call setEnabled(false) on it?

    If you need more help, I think you're going to have to explain the rationale behind the requirement. You might also consider posting a SSCCE.
    SSCCE : Java Glossary

    db
    That won't be necessary. Your answer is more than helpful.

Similar Threads

  1. Replies: 2
    Last Post: 10-01-2010, 07:18 PM
  2. SetBackground for JComponent
    By tulasi.neppali in forum AWT / Swing
    Replies: 5
    Last Post: 09-12-2010, 10:14 PM
  3. Random size of JComponent
    By Karl-von-bahnhof in forum AWT / Swing
    Replies: 0
    Last Post: 04-13-2010, 08:14 PM
  4. JAXB Bindings - error while compiling Schema(s)
    By Stipejava in forum NetBeans
    Replies: 0
    Last Post: 10-30-2009, 12:15 PM
  5. genesis 3.0-RC2: Swing, SWT and Thinlet bindings
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-24-2007, 10:12 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
  •