TitledPane中的焦点遍历政策 [英] Focus Traversal Policy in TitledPane
问题描述
在 JavaFX:如何更改焦点遍历政策? Alexander Kirov展示如何自定义JavaFX应用程序的焦点遍历策略。它工作正常,但不适用于 TitledPane
s。如果 TitledPane
中的节点具有焦点,则不会调用设置的 TraversalEngine
。
On JavaFX: How to change the focus traversal policy? Alexander Kirov show how to customize the focus traversal policy for a JavaFX application. It works fine, but not for TitledPane
s. If a node in a TitledPane
has focus, the setted TraversalEngine
is not called.
以下是显示此现象的完整示例:
Here is a full example to show this phenomenon:
package org.example;
import com.sun.javafx.scene.traversal.Direction;
import com.sun.javafx.scene.traversal.TraversalEngine;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class FocusTest extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
// Create UI
final VBox root = new VBox();
final Button foo = new Button("foo");
foo.setId("foo");
root.getChildren().add(foo);
final Button bar = new Button("bar");
bar.setId("bar");
final Pane content = new Pane();
content.getChildren().add(bar);
final TitledPane tp = new TitledPane("tp", content);
root.getChildren().add(tp);
// Set TraversalEngine
final TraversalEngine te = new TraversalEngine(root, false) {
@Override
public void trav(Node owner, Direction direction) {
System.out.printf("trav owner: %s, direction: %s%n",
owner.getId(), direction);
switch (direction) {
case DOWN:
case RIGHT:
case NEXT:
if (owner == foo) {
bar.requestFocus();
} else if (owner == bar) {
foo.requestFocus();
}
break;
case LEFT:
case PREVIOUS:
case UP:
if (owner == foo) {
bar.requestFocus();
} else if (owner == bar) {
foo.requestFocus();
}
break;
}
}
};
root.setImpl_traversalEngine(te);
// Show Scene
final Scene scene = new Scene(root);
primaryStage.setHeight(200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
场景的根是 VBox
并设置自定义 TraversalEngine
。如果按钮 foo
有焦点,我按[Tab]然后调用 te.trav
并设置焦点到 bar
。这就是我的预期。但是当 bar
有焦点时,不会调用 te.trav
。 bar
是 TitledPane
的子项。此行为显示在 1 中。
The root of the scene is a VBox
and the custom TraversalEngine
is set to it. If the button foo
has the focus and I press [Tab] then te.trav
is called and the focus is set to bar
. That's how I expected. But when bar
has the focus, te.trav
is not called. bar
is a child of the TitledPane
. This behaviour is shown in 1.
有没有人可以解决这个问题?
Has anyone a solution for this?
推荐答案
这是一个处理 TitledPane $ c的棘手解决方案$ c> s:
@SuppressWarnings("deprecation")
private static void registerTraversalEngine(final Parent parent,
final TraversalEngine te) {
parent.setImpl_traversalEngine(te);
for (Node child : parent.getChildrenUnmodifiable()) {
if (child instanceof Parent) {
registerTraversalEngine((Parent) child, te);
}
}
if (parent instanceof TitledPane) {
final TitledPane tp = (TitledPane) parent;
if (tp.getContent() instanceof Parent) {
registerTraversalEngine((Parent) tp.getContent(), te);
}
}
}
我认为问题与 TitltedPane
s,内容不在子集中:
I think the problem with TitltedPane
s is, that the content is not in the children set:
TitledPane.getChildrenUnmodifiable().contains(TitledPane.getContent()) is always false
这篇关于TitledPane中的焦点遍历政策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!