tabPaine更改后的javafx setFocus [英] javafx setFocus after tabPaine change

查看:214
本文介绍了tabPaine更改后的javafx setFocus的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

有tabPane选项卡确定。

在第一个选项卡中有一个文本字段。启动应用程序时,我可以专注于此字段。
更改选项卡并返回第一个选项卡后,我希望焦点位于此文本字段(条形码阅读器应在此字段中处于活动状态),而无需使用鼠标选择字段。

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

(无法发布代码)

我可以触发第一个标签的en事件。
但是field.requestFocus();不起作用。可能是因为这个方法来之前渲染文本域。

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.

所以这是我的问题:

如何在点击后设置对焦的控件TabPane中的选项卡?

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

推荐答案

如果您处理鼠标释放事件,它可以工作:( doFocus只在a处理时启用requestFocus处理选项卡选择之前已更改,否则每次单击某些选项时都会启动TabPane中的e。)

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);
        }
    });

当TabPane具有焦点时,可以使用光标键更改标签选择,并且TextFields也赢了采用基于选择的方法来关注焦点。如果你需要的话,这也许应该处理。

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.

(最近我遇到了类似的问题。我注意到,当你按下鼠标按钮时,TabPane会立即切换标签我的猜测是,基于选择的方法请求在鼠标按下后立即关注TextField,但是持续的鼠标按下将焦点移回TabPane。或者甚至可能改变选择的单个鼠标按下事件导致焦点转移回到TabPane。然而,我对原因的假设可能不正确,因为我是JavaFX的新手。)

(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.)

编辑:这种处理当然不是最佳的。例如,如果使用键更改选项卡,则会启用doFocus,然后单击TabPane中的任何位置将触发requestFocus调用。我认为应该提到这一点。

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.

这篇关于tabPaine更改后的javafx setFocus的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆