javafx setFocus after tabPaine change

五迷三道 提交于 2020-01-17 08:25:54

问题


Problem:
Have tabPane tabs OK.
In the first tab there is a text field. I am able to get focus on this field when starting the application. After changing the tabs and coming back to the first tab I want focus to be on this textfield (barcodereader should be active in this field) without having to select the field with the mouse.

I am able to catch event from tabs with

 tp.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Tab>()
   { etc

(could not post with code)

and I am able to trigger en event for the first tab. But field.requestFocus(); does not work. Probably because this method comes before rendering the textfield.

So here is my question:

How do you set focus on a control after clicking tabs in TabPane?


回答1:


If you handle the mouse release event, it works: (The doFocus enables the requestFocus handling only when a tab selection changed before, otherwise it kicks in every time you click somewhere in the 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 the TabPane has focus, one can change tab selection with the cursor keys and there the TextFields also won't get the focus with selection based approach. This probably should be handled too, if you need it.

(Recently I had a similar problem. I noticed, that the TabPane switches tabs immediately when you press the mouse button. My guess would be, that the selection based approach requests focus on the TextField right after mouse down, but the continued mouse down steals the focus back to the TabPane. Or maybe even the single mouse down event which changes selection causes the focus to go back to TabPane. However, my assumptions regarding the reasons may not be correct, as I am a newbie to JavaFX.)

EDIT: That handling certainly is not optimal. For instance, if you change tabs with the keys, the doFocus will be enabled and then clicking anywhere in the TabPane will trigger the requestFocus call. I thought this should be mentioned.




回答2:


Also, take a look at my solution for setting focus on TextArea, when user changes selected tab(using mouse or keyboard) https://stackoverflow.com/a/19046535/2791746



来源:https://stackoverflow.com/questions/15646724/javafx-setfocus-after-tabpaine-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!