java - Binding a key to a JButton -
this question has answer here:
i have been looking answer attach keybinding jbutton many-many hours, , still didn't manage it. have following simple program consisting of 2 classes. tried using getinputmap() , getactionmap() several ways, without success. want following: when press key "1" on keyboard, press jbutton btn1, , when press key "2", press jbutton btn2 (and 1 or 2 appear on jlabel consequently).
//class1://
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class event06 extends jframe { event06b base = new event06b(this); jbutton btn1 = new jbutton("1"); jbutton btn2 = new jbutton("2"); jlabel label = new jlabel(""); public event06() { super(); setbounds(300,300, 200,150); setresizable(true); settitle("button keybinding"); setdefaultcloseoperation(jframe.exit_on_close); btn1.addactionlistener(base); btn2.addactionlistener(base); flowlayout flo = new flowlayout(flowlayout.center); setlayout(flo); add(btn1); add(btn2); add(label); setvisible(true); } public static void main(string[] args) { event06 window = new event06(); } } //class 2:// import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class event06b implements actionlistener { event06 gui; public event06b (event06 in) { gui = in; } public void actionperformed(actionevent pressed) { object source = pressed.getsource(); if (source == gui.btn1) {gui.label.settext("1");} else if (source == gui.btn2) {gui.label.settext("2");} } }
update: (i still don't have reputation of 15 yet, cannot answer own question, , far know cannot post codes or long answers in comments, i'm modifying question instead).
what managed find out using keylisteners work, if no button has focus. see following example:
//in class 1:// flowlayout flo = new flowlayout(flowlayout.center); setlayout(flo); add(btn1); btn1.setenabled(false); btn2.setenabled(false); add(btn2); add(label); btn1.addactionlistener(base); btn2.addactionlistener(base); addkeylistener(base);
here, 2 buttons, btn1 , btn2 disabled, don't have focus, instead window on focus. that's why keylistener can work:
public void keypressed (keyevent evt) { int keycode = evt.getkeycode(); gui.label.settext(integer.tostring(keycode)); } public void keyreleased(keyevent txt) {} public void keytyped(keyevent txt) {}
in case, tha jlabel in class1 shows keycode of key has been pressed. (note, can keycode under keypressed method, , not under keytyped method - latter getting key character using getkeychar. better use getkeycode instead of getkeychar, since specific keys have keycode, not keychar).
for buttons use actionperformed method:
public void actionperformed(actionevent pressed) { object source = pressed.getsource(); if (source == gui.btn1) {gui.label.settext("1");} else if (source == gui.btn2) {gui.label.settext("2");} }
since buttons disabled here, doesn't work. far, couldn't connect keycode actionperformed method. in example presented veluria, actionperformed method part of abstractaction, , inputmaps , actionmaps used there. seems correct answer here, though error, when i'm trying use suggestion: error: identifier expected
yeah, found solution!!!: (first show code, explain modified).
//class 1://
import javax.swing.*; import java.awt.event.*; import java.awt.*; public class event06 extends jframe { event06b base = new event06b(this); jbutton btn1 = new jbutton("1"); jbutton btn2 = new jbutton("2"); jlabel label = new jlabel(""); public event06() { super(); setbounds(300,300,250,75); setresizable(false); settitle("buttons key"); setdefaultcloseoperation(jframe.exit_on_close); flowlayout flo = new flowlayout(flowlayout.center); setlayout(flo); add(btn1); btn1.setenabled(true); btn2.setenabled(true); add(btn2); add(label); btn1.addactionlistener(base.act); btn1.getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke('1'), "pressed"); btn1.getactionmap().put("pressed", base.act); btn2.addactionlistener(base.act); btn2.getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke('2'), "pressed"); btn2.getactionmap().put("pressed", base.act); setvisible(true); } public static void main(string[] args) { event06 window = new event06(); } }
class2:
import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; public class event06b { event06 gui; public event06b (event06 in) { gui = in; } action act = new abstractaction() { @override public void actionperformed(actionevent pressed) { object source = pressed.getsource(); if (source == gui.btn1) {gui.label.settext("1");} else if (source == gui.btn2) {gui.label.settext("2");} } }; }
so, did following: - added actionlistener in class 1 btn1 , btn2:
btn1.addactionlistener(base.act);
i used inputmap:
btn1.getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke('1'), "pressed");
two things note here: have put "jcomponent.when_in_focused_window" text in round brackets, input key when button not have focus. other thing is, when use getkeystroke('1'), have give character, use ' ' instead of " ". (i saw "" being used in many examples).
then used actionmap, so:
btn1.getactionmap().put("pressed", base.act);
please note, here, , @ actionlistener, referring other class using base.act (base refers class2, , act refers action inside)
in class2, put actionperformed method inside abstractaction, , used @override. don't know, why @override has used, because works without it.
i'm sorry, if misinterpreted , didn't give right explanation. i'm hobby programmer, having no education in computer science. hope, question , provided answer might bring many others being in same boat.
here's 1 way it:
action action = new abstractaction("1") { @override public void actionperformed(actionevent e) { label.settext("1"); } }; action.putvalue(action.accelerator_key, keystroke.getkeystroke("1")); btn1.setaction(action); btn1.getactionmap().put("setoneaction", action); btn1.getinputmap(jcomponent.when_in_focused_window).put( (keystroke) action.getvalue(action.accelerator_key), "setoneaction");
Comments
Post a Comment