Posts

Showing posts with the label jframe

KeyListener/KeyBindings and order of adding components

KeyListener/KeyBindings and order of adding components In my program a JPanel component which is operated by listener using KeyBindings (changed that from KeyListeners already after reading about issues with focusing) is added to a JFrame. Task of this app is to simply move drawn figure around using arrow keys. This works perfectly until i add another component before drawing board (JPanel with three buttons). I tested both Keybindings and KeyListener and both methods have the same issue. If I add components after drawing board keybindings starts to work again. Here is my UI class where i add components. EDIT: removed uncleaned code I would like to understand Java a bit more so any answer is very appreciated. Edit: I cleaned my code and created "minimal" example of the problem. Thank you Andrew for heads up. Program should print "movingup" in console after pressing arrow up. The problem occurs here: container.add(buttons); //after adding this Key Bindings stops work...