On JavaFX: How to change the focus traversal policy? Alexander Kirov演示瞭如何爲JavaFX應用程序定製焦點遍歷策略。它工作正常,但不適用於TitledPane
s。如果TitledPane
中的節點具有焦點,則不會調用設置的TraversalEngine
。TitledPane中的焦點遍歷策略
這是一個完整的例子來說明這一現象:
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中。
有沒有人爲此提供解決方案?