Results 1 to 3 of 3
- 10-13-2010, 10:05 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
can one disable key bindings on a JComponent?
Suppose that I've got a set of keys bound to a JComponent. Something like this:
Is there a simple way to temporarily disable the key bindings? Something likeJava 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)); }
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.Java Code:myComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).setEnabled(false);
- 10-14-2010, 04:35 AM #2
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
- 10-14-2010, 04:44 PM #3
Member
- Join Date
- Jun 2010
- Posts
- 86
- Rep Power
- 0
Similar Threads
-
Using JComponent methods on a JComponent that was found using .getComponents()
By tashimoto in forum New To JavaReplies: 2Last Post: 10-01-2010, 07:18 PM -
SetBackground for JComponent
By tulasi.neppali in forum AWT / SwingReplies: 5Last Post: 09-12-2010, 10:14 PM -
Random size of JComponent
By Karl-von-bahnhof in forum AWT / SwingReplies: 0Last Post: 04-13-2010, 08:14 PM -
JAXB Bindings - error while compiling Schema(s)
By Stipejava in forum NetBeansReplies: 0Last Post: 10-30-2009, 12:15 PM -
genesis 3.0-RC2: Swing, SWT and Thinlet bindings
By levent in forum Java SoftwareReplies: 0Last Post: 05-24-2007, 10:12 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks