tabpanel - javafx setFocus after tabPaine change -


problem:
have tabpane tabs ok.
in first tab there text field. able focus on field when starting application. after changing tabs , coming first tab want focus on textfield (barcodereader should active in field) without having select field mouse.

i able catch event tabs with

 tp.getselectionmodel().selecteditemproperty().addlistener(new changelistener<tab>()    { etc 

(could not post code)

and able trigger en event first tab. field.requestfocus(); not work. because method comes before rendering textfield.

so here question:

how set focus on control after clicking tabs in tabpane?

if handle mouse release event, works: (the dofocus enables requestfocus handling when tab selection changed before, otherwise kicks in every time click somewhere in tabpane.)

    final simplebooleanproperty dofocus = new simplebooleanproperty(false);     tabpane.setonmousereleased(new eventhandler<event>() {         @override         public void handle(event event) {             if (!dofocus.get()) {                 return;             }             dofocus.set(false);             switch (tabpane.selectionmodelproperty().getvalue().selectedindexproperty().intvalue()) {             case 0: tf1b.requestfocus(); break;             case 1: tf2a.requestfocus(); break;             default: break;             }         }     });     tabpane.selectionmodelproperty().getvalue().selectedindexproperty().addlistener(new changelistener<number>() {         @override         public void changed(observablevalue<? extends number> observable,                 number oldvalue, number newvalue) {             dofocus.set(true);         }     }); 

when tabpane has focus, 1 can change tab selection cursor keys , there textfields won't focus selection based approach. should handled too, if need it.

(recently had similar problem. noticed, tabpane switches tabs when press mouse button. guess be, selection based approach requests focus on textfield right after mouse down, continued mouse down steals focus tabpane. or maybe single mouse down event changes selection causes focus go tabpane. however, assumptions regarding reasons may not correct, newbie javafx.)

edit: handling not optimal. instance, if change tabs keys, dofocus enabled , clicking anywhere in tabpane trigger requestfocus call. thought should mentioned.


Comments