java - Why JScrollBar doesn't respond to arrow keystrokes when coexist with editable component? -
(things clear in code after description)
i have program contains jscrollbar, jtextarea, jtextfields, jbuttons , other things. (i added sscce codes)
ok, added key bindings left , right jbuttons of jscrollbar , used getinputmap, , getactionmap achieve that.
if have jscrollbar in gui program without editable components such jtextfield or jtextarea, keystrokes keyevent.vk_left , keyevent.vk_right work ok (as expected)
but if have editable component jscrollbar not respond keyevent.vk_left , keyevent.vk_right (not expected) why happen!?
more strange thing if chose different keystrokes bind such keyevent.vk_s left , keyevent.vk_f right, work!! why?
how fix, how make key bindings of left , right arrows jscrollbar jbuttons while editable component coexist?
these 2 working codes in sscce style. first contains jscrollbar without editable components, second contains jscrollbar jtextarea.
the first work ok respond left , right arrows of keyboard. (it doesn't contain editable components)
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class scrolltest extends jpanel { jpanel panel; jscrollbar scrollbar; jbutton sliderleftbutton; jbutton sliderrightbutton; public scrolltest() { scrollbar = new jscrollbar(jscrollbar.horizontal, 0, 6, 0, 300); sliderleftbutton = (jbutton) scrollbar.getaccessiblecontext().getaccessiblechild(1); sliderleftbutton.getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke(keyevent.vk_left, 0), "leftmove"); sliderleftbutton.getactionmap().put("leftmove", leftmove); sliderrightbutton = (jbutton) scrollbar.getaccessiblecontext().getaccessiblechild(0); sliderrightbutton.getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke(keyevent.vk_right, 0), "rightmove"); sliderrightbutton.getactionmap().put("rightmove", rightmove); panel = new jpanel(new gridlayout(2, 0)); panel.add(scrollbar); this.setlayout(new borderlayout()); this.add(panel, borderlayout.north); } abstractaction leftmove = new abstractaction() { @override public void actionperformed(actionevent ae) { int increment = scrollbar.getblockincrement(); int current = scrollbar.getvalue(); current -= increment; scrollbar.setvalue(current); system.out.println("left"); } }; abstractaction rightmove = new abstractaction() { @override public void actionperformed(actionevent ae) { int increment = scrollbar.getblockincrement(); int current = scrollbar.getvalue(); current += increment; scrollbar.setvalue(current); system.out.println("right"); } }; private static void createandshowgui() { jframe frame; frame = new jframe("scroll test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(880, 100); frame.add(new scrolltest(), borderlayout.center); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { uimanager.put("swing.boldmetal", boolean.false); createandshowgui(); } }); } } the second not work not respond left , right arrows of keyboard. (it contain editable components - 1 jtextarea)
the more strange thing
replace keyevent.vk_left keyevent.vk_s , replace keyevent.vk_right keyevent.vk_f. s work left , f work right! why didn't work left , right arrows.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class scrolltest extends jpanel { jpanel panel; jpanel panel2; jscrollbar scrollbar; jbutton sliderleftbutton; jbutton sliderrightbutton; public scrolltest() { scrollbar = new jscrollbar(jscrollbar.horizontal, 0, 6, 0, 300); sliderleftbutton = (jbutton) scrollbar.getaccessiblecontext().getaccessiblechild(1); sliderleftbutton.getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke(keyevent.vk_left, 0), "leftmove"); sliderleftbutton.getactionmap().put("leftmove", leftmove); sliderrightbutton = (jbutton) scrollbar.getaccessiblecontext().getaccessiblechild(0); sliderrightbutton.getinputmap(jcomponent.when_in_focused_window).put(keystroke.getkeystroke(keyevent.vk_right, 0), "rightmove"); sliderrightbutton.getactionmap().put("rightmove", rightmove); panel = new jpanel(new gridlayout(2, 0)); panel.add(scrollbar); panel2 = new jpanel(new gridlayout(1, 0)); panel2.add(new jtextarea(50, 10)); this.setlayout(new borderlayout()); this.add(panel, borderlayout.north); this.add(panel2, borderlayout.south); } abstractaction leftmove = new abstractaction() { @override public void actionperformed(actionevent ae) { int increment = scrollbar.getblockincrement(); int current = scrollbar.getvalue(); current -= increment; scrollbar.setvalue(current); system.out.println("left"); } }; abstractaction rightmove = new abstractaction() { @override public void actionperformed(actionevent ae) { int increment = scrollbar.getblockincrement(); int current = scrollbar.getvalue(); current += increment; scrollbar.setvalue(current); system.out.println("right"); } }; private static void createandshowgui() { jframe frame; frame = new jframe("scroll test"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(880, 100); frame.add(new scrolltest(), borderlayout.center); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(new runnable() { public void run() { uimanager.put("swing.boldmetal", boolean.false); createandshowgui(); } }); } }
this because jtextarea stealing key strokes:
textpane.getinputmap().put(keystroke.getkeystroke(keyevent.vk_right, 0), "pressed right"); textpane.getactionmap().put("pressed right", rightmove); have @ basictextui.installkeyboardactions()
Comments
Post a Comment