java - javafx don't loss the focus when click other node -
i have vbox on left of rootpane,and vbox on right of rootpane. leftvobx have 3 textarea nodes,rightvbox have colorpicker; want: when choose 1 textarea node on leftvbox, click colorpicker on rightvbox, current textarea don't loss focus.
my idea same "scene builder" , when select note in workspace, handle right slide function area(like change color,set size , font),the note not lose focus, action know node in workspace need handle.
code: vbox leftbox = new vbox(); vbox rightbox = new vbox();
leftbox.setprefsize(200, 250); rightbox.setprefsize(200, 250); leftbox.setstyle("-fx-background-color:blue"); button btn1 = new button("first"); button btn2 = new button("second"); button btn3 = new button("third"); colorpicker colorpicker = new colorpicker(); leftbox.getchildren().addall(btn1,btn2,btn3); rightbox.getchildren().add(colorpicker); colorpicker.setonaction(e->{ if(btn2.isfocused()){ btn2.settext("color changed."); } }); hbox root = new hbox(); root.getchildren().addall(leftbox,rightbox); scene scene = new scene(root, 400, 250); primarystage.settitle("hello world!"); primarystage.setscene(scene); primarystage.show();
there no way have 2 nodes focused afaik. weird, since every keyboard event etc go 2 nodes. in javafx scene builder well, can try it. click on in scenebuilder , press delete, deleted. press on in scenebuilder , on right , press delete , item selected first not deleted. because doesn't have focus. scenebuilder shows had last selected.
to solve problem. make new variable button lastfocused; , make listeners on other buttons focusedproperty this:
btn2.focusedproperty().addlistener((observablevalue<? extends boolean> observable, boolean oldvalue, boolean newvalue) -> { if(!newvalue) lastfocused = btn2; }); this should store button in new variable looses focus. looses focus when focus color picker, variable have button selected when color picker got selected.
now in color picker should work:
colorpicker.setonaction(e->{ lastfocused.settext("color changed."); lastfocused.requestfocus(); }); i have not tested code.
Comments
Post a Comment