JavaFX:如何改变焦点遍历策略? [英] JavaFX: How to change the focus traversal policy?

查看:165
本文介绍了JavaFX:如何改变焦点遍历策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在JavaFX中更改焦点遍历策略,就像在AWT中一样?



因为我的两个 HBox es是错误的。在一般的情况下,导航是按容器顺序,按照孩子的顺序,或者按箭头键来完成的。 你可以改变节点的顺序 - 在这种情况下它将是你的最佳解决方案。



JFX有一个关于遍历引擎策略替换的后门:



可以继承内部类com.sun.javafx.scene.traversal.TraversalEngine

  engine = new TraversalEngine(this,false){
@Override public void trav(Node owner,Direction dir){
//做任何你想做的事
}
} ;

并使用

  setImpl_traversalEngine(发动机); 

调用应用该引擎。

你可以观察OpenJFX的代码,理解它是如何工作的,以及你可以做什么。

要小心:它是一个内部API,很可能可能在不久的将来改变。所以不要依赖这个(不管怎么说,你不能依赖这个官方的)。

样例实现:

  public void start(Stage stage)throws Exception {
final VBox vb = new VBox();

最终按钮button1 =新建按钮(按钮1);
最终按钮button2 =新建按钮(按钮2);
最终按钮button3 =新建按钮(按钮3);

TraversalEngine发动机=新TraversalEngine(VB,假){
@覆盖
公共无效TRAV(节点节点,方向drctn){
INT指数= vb.getChildren ().indexOf(节点);
$ b $ switch(drctn){
case DOWN:
case RIGHT:
case NEXT:
index ++;
break;
case左:b $ b case上一个:
case上一个:
index--; (index <0){
index = vb.getChildren()。size() - 1;
}

if
}
index%= vb.getChildren()。size();

System.out.println(Select<+ index +>);

vb.getChildren()。get(index).requestFocus();
}
};

vb.setImpl_traversalEngine(engine);

vb.getChildren()。addAll(button1,button2,button3);
场景场景=新场景(vb);
stage.setScene(scene);
stage.show();



$ b $ p
$ b

这将需要强大的分析能力, b $ b

Is it possible in JavaFX to change the focus traversal policy, like in AWT?

Because the traversal order for two of my HBoxes is wrong.

解决方案

In common case the navigation is done in a container order, in order of children, or according to arrow keys pressing. You can change order of nodes - it will be the optimal solution for you in this situation.

There is a back door in JFX about traversal engine strategy substitution :

you can subclass the internal class com.sun.javafx.scene.traversal.TraversalEngine

engine = new TraversalEngine(this, false) {
            @Override public void trav(Node owner, Direction dir) {
                // do whatever you want
            }
        };

And use

setImpl_traversalEngine(engine); 

call to apply that engine.

You can observe the code of OpenJFX, to understand, how it works, and what you can do.

Be very careful : it is an internal API, and it is likely to change, possibly, in the nearest future. So don't rely on this (you cannot rely on this officialy, anyway).

Sample implementation :

public void start(Stage stage) throws Exception {
    final VBox vb = new VBox();

    final Button button1 = new Button("Button 1");
    final Button button2 = new Button("Button 2");
    final Button button3 = new Button("Button 3");

    TraversalEngine engine = new TraversalEngine(vb, false) {
        @Override
        public void trav(Node node, Direction drctn) {
            int index = vb.getChildren().indexOf(node);

            switch (drctn) {
                case DOWN:
                case RIGHT:
                case NEXT:
                    index++;
                    break;
                case LEFT:
                case PREVIOUS:
                case UP:
                    index--;
            }

            if (index < 0) {
                index = vb.getChildren().size() - 1;
            }
            index %= vb.getChildren().size();

            System.out.println("Select <" + index + ">");

            vb.getChildren().get(index).requestFocus();
        }
    };

    vb.setImpl_traversalEngine(engine);

    vb.getChildren().addAll(button1, button2, button3);
    Scene scene = new Scene(vb);
    stage.setScene(scene);
    stage.show();
}

It will require strong analitical skills for common case ;)

这篇关于JavaFX:如何改变焦点遍历策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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