JavaFX节点的Focus Listener [英] Focus Listener for JavaFX Nodes

查看:853
本文介绍了JavaFX节点的Focus Listener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaFX的初学者。我真的陷入了困境。 :(抱歉,如果我的英语很差。



我的JavaFX程序中有两个堆栈窗格。我想在这两个堆栈窗格中添加一个焦点监听器。 / p>

当我点击一个堆栈窗格时,它应该激活此堆栈窗格的焦点获取方法。



一旦我点击另一个堆栈窗格,第一个堆栈窗格应该调用它的焦点丢失方法,并且应该调用当前堆栈窗格的focus gain方法。就像我们在Swing包中有焦点事件一样。



目前我试过这个:

  stackPane.focusedProperty() .addListener(new ChangeListener< Boolean>(){

@Override
public void changed(ObservableValue<?extends Boolean> observable,Boolean oldValue,Boolean newValue){
if(
if( newValue.booleanValue()){
focusGained(stackPane);
} else {
focusLost(stackPane);
}
}
});

private void focusGained(StackPane stackPane){
System.out.println(Focus Gained。);
}

private void focusLost(StackPane stackPane){
System.out.println(Focus Lost。);
}

我还尝试在堆栈窗格上设置焦点遍历属性,即

  stackPane.setFocusTraversable(true); 

这些都无法正常工作。当我运行它时,输出只显示这3行,无论我点击堆栈窗格多少次。

 焦点获得。 
专注迷失。
焦点获得。

请帮助。

解决方案

嗯,这有点晚了,但可能有助于其他人。
这很好用:

  root.focusedProperty()。addListener((ObservableValue<?extends Boolean> observable,Boolean oldValue,Boolean newValue) - > {
focusState(newValue);
});

private void focusState(boolean value){
if(value){
System.out.println(Focus Gained);
}
else {
System.out.println(Focus Lost);
}
}


I am a beginner in JavaFX. I am really stuck at this point. :( And sorry if my English is poor.

I have two stack panes in my JavaFX program. I want to add a focus listener to both of these stack panes.

It should be such that, when I click on one stack pane, it should activate the focus gained method for this stack pane.

Once I click on another stack pane, the 1st stack pane should give a call to its focus lost method, and the current stack pane's focus gained method should be called. Just like we have focus events in the Swing Package.

Currently I have tried this:

stackPane.focusedProperty().addListener(new ChangeListener<Boolean>() {

                @Override
                public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                    if (newValue.booleanValue()) {
                        focusGained(stackPane);
                    } else {
                        focusLost(stackPane);
                    }
                }
            });

private void focusGained(StackPane stackPane){
    System.out.println("Focus Gained.");
}

private void focusLost(StackPane stackPane){
    System.out.println("Focus Lost.");
}

I have also tried to set the focus traversable property on the stack pane i.e.

stackPane.setFocusTraversable(true);

These are not working properly. When I run it, the output only shows these 3 lines, no matter how many times I click on the stack panes.

Focus Gained.
Focus Lost.
Focus Gained.

Please help.

解决方案

Well, it's a bit late but it might help others. This works fine:

root.focusedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
    focusState(newValue);
});

private void focusState(boolean value) {
    if (value) {
        System.out.println("Focus Gained");
    }
    else {
        System.out.println("Focus Lost");
    }
}

这篇关于JavaFX节点的Focus Listener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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