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

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
Tmux Scroll Up/Down Page using Ctrl-b and Ctrl-f
How to create key binding to set XML syntax?
How to bypass a bindkey directive in GNU screen?

Categories

HOME
fortran
ldap
pelican
grpc
casting
stdout
checkpoint
browserstack
pip
samsung-gear-s2
ionic-native
oms
elastalert
urllib2
fastreport
luis
nsoperation
web-parts
react-jsx
jq
fibonacci
google-drive-android-api
connection-pooling
aws-iot
oracle10g
foxpro
language-detection
keil
log4net-configuration
storefront
angularjs-resource
minitest
kdevelop
abide
iterm2
linkurious
cowboy
ng-lightning
quartz
contenteditable
qt-quick
rexx
intervention
niagara-ax
encoder
azure-data-catalog
spring-webflow-2
stanford-nlp-server
denodo
ear
cardview
stringbuilder
easy68k
mongodb-3.3
geokit
syslog4j
patching
aws-kms
python-rq
sqlxml
yfiles
encode
kramdown
gnu-sort
self-join
openhab
borland-c++
eaaccessory
radgrid
random-access
report-builder2.0
unityvs
react-os
api-eveonline
go-gin
breeze-sharp
powerpoint-2010
agent-based-modeling
visual-c#-express-2010
hotlinking
internet-radio
logo-lang
ekevent
xml-validation
jfilechooser
cab
nokogiri
maven-webstart-plugin
android-library
panelgrid
qmainwindow
msbuild-task
setcookie
big-endian
motorola-droid
commonsware
automapper-2
gmagick
html5-animation
text-manipulation
nscharacterset
webdb
dojox.gfx
file-storage
script-tag
zend-acl
cons

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