key-bindings


KeyBindings in Java stop working after component is remade


My JPanel's KeyBindings work fine until I remove the JPanel from my JFrame, make a new JPanel object and then add that to the JFrame.
The KeyBinding methods run again on the new JPanel object but the Keys don't do anything.
Here are two classes I made to show my problem:
public class KeyBindingsTester {
static JFrame jf;
static KeyBindingPanel kp;
public static void main(String[] args){
new KeyBindingsTester();
}
public KeyBindingsTester(){
jf = new JFrame();
kp = new KeyBindingPanel();
jf.add(kp, BorderLayout.CENTER);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setVisible(true);
}
public static void reset(){
jf.remove(kp);
kp = new KeyBindingPanel();
jf.add(kp,BorderLayout.CENTER);
jf.setVisible(true);
}
}
--------- AND The Panel:
public class KeyBindingPanel extends JPanel{
public KeyBindingPanel(){
setUpKeyBindings();
System.out.println("Keybindings set up");
}
public void setUpKeyBindings(){
addKeyBinding("B");
addKeyBinding("R");
}
public void addKeyBinding(String key){
getInputMap().put(KeyStroke.getKeyStroke(key), key);
getActionMap().put(key, new KeyBindingAction(key));
}
public class KeyBindingAction extends AbstractAction{
String action;
public KeyBindingAction(String actionName){
action = actionName;
}
public void actionPerformed(ActionEvent e){
if(action.equals("R")){
KeyBindingsTester.reset();
}
if(action.equals("B")){
System.out.println("BBB");
}
}
}
}
The problem was that the focus wasn't on the JPanel in question.
Changing this: getInputMap().put(KeyStroke.getKeyStroke(key), key);
to this: getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key), key);

Related Links

Reassign Chrome OS keybindings
home\ end keys in xterm not behaving correctly
Exporting ALL visual studio key actions (keyactions) for keybindings
How to setup a custom keybinding to run a script or execute a command in Atom editor?
Bind key prefix + ; to open command-prompt in tmux
keybinding to interchange keyboard language
Is there a move cursor to nearest '=' code navigation in IntelliJ?
Disabling Mousewheel Bind
Fish shell ESC/META keybinding with iterm2 on os x yosemite
XMonad: Is there a way to bind a simultaneously triggered keychord?
Simple key binding issue
unable to get C-PGUP or C-PGDOWN key codes in tmux
How do I override a default keybinding in LightTable?
Is it possible to change key bindings in Sublime Text dynamically from a plugin?
Ace Editor - Change CTRL+H keybinding
KeyBindings in Java stop working after component is remade

Categories

HOME
max
kendo-ui-angular2
stata
sql-server-2014
network-programming
oracle-sqldeveloper
microcontroller
phantomjs
android-ndk
sugarcrm
jshell
uml-designer
kohana
ios-universal-links
yui
tag-cloud
filehelpers
wallet
google-calendar-api3
single-page-application
ejb-3.1
combinations
angular2-pipe
inline-assembly
aws-iot
rvm
windowserror
chartist.js
rancher
sasl
ipython-parallel
ios9
wordpress-rest-api
node-soap
certbot
intersection
scip
stochastic-process
kepserverex
mongoid5
form-fields
mapquest
motion
aspose.pdf
java-5
cakephp-3.2
ack
reqif
runtime-permissions
cancan
android-vectordrawable
liferay-ide
stringbuilder
turbolinks
tinybox2
db2-zos
diameter-protocol
mrunit
3d-rendering
skbio
ooad
sqldataadapter
biginsights
cloud-storage
firewire
stderr
system.diagnostics
httpmodule
accpac
connection-timeout
dockerpy
mathics
uiblureffect
clearcanvas
xmlserializer
atg-dynamo
maven-javadoc-plugin
latex-suite
pymel
system.transactions
linqdatasource
screensharing
wpml
derived-class
aspmenu-control
optimistic-concurrency
towerjs
pex-and-moles
service-layer
n2
noise-reduction
marathontesting
lgpl
manuals
retrospectiva
cross-cutting-concerns
office-2003

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App