JavaFX : 텍스트 영역을 참조하여 텍스트 영역이있는 탭을 어떻게 선택할 수 있습니까?
어떤 도움이라도 대단히 감사하겠습니다, 감사합니다!
Node
스타일 클래스 tab-content-area
가 부모 인 노드 를 찾을 때까지 노드 계층 구조를 통해 올라갈 수 있습니다. 이것은의 콘텐츠 노드입니다 Tab
. 이 정보를 바탕으로 적절한 것을 찾을 수 있습니다 Tab
.
public static Tab findTab(Node node, TabPane tabPane) {
if (tabPane == null) {
throw new IllegalArgumentException();
}
// find content node that contains node
Node parent = node.getParent();
while (parent != null && !parent.getStyleClass().contains("tab-content-area")) {
node = parent;
parent = node.getParent();
}
// root reached before reaching the content of a tab
if (parent == null) {
return null;
}
// find appropriate tab
for (Tab tab : tabPane.getTabs()) {
if (tab.getContent() == node) {
return tab;
}
}
return null;
}
@Override
public void start(Stage primaryStage) {
TabPane tabPane = new TabPane();
TextArea textArea1 = new TextArea();
TextArea textArea2 = new TextArea();
Tab tab1 = new Tab("tab1", textArea1);
// wrap some parent to show this is working too
Tab tab2 = new Tab("tab2", new StackPane(textArea2));
tabPane.getTabs().addAll(tab1, tab2);
ChangeListener<String> textChangeListener = (observable, oldValue, newValue) -> {
// get node containing the property
Node sourceNode = (Node) ((ReadOnlyProperty) observable).getBean();
// find corresponging tab and select it
Tab tab = findTab(sourceNode, tabPane);
if (tab != null) {
tabPane.getSelectionModel().select(tab);
}
};
textArea1.textProperty().addListener(textChangeListener);
textArea2.textProperty().addListener(textChangeListener);
// add some text to the text areas alternatingly
Timeline timeline = new Timeline(new KeyFrame(Duration.seconds(2), new EventHandler<ActionEvent>() {
private int num = 1;
@Override
public void handle(ActionEvent event) {
textArea1.appendText("\n" + num);
num += 2;
}
}), new KeyFrame(Duration.seconds(4), new EventHandler<ActionEvent>() {
private int num;
@Override
public void handle(ActionEvent event) {
textArea2.appendText("\n" + num);
num += 2;
}
}));
timeline.setCycleCount(Animation.INDEFINITE);
timeline.play();
Scene scene = new Scene(tabPane);
primaryStage.setScene(scene);
primaryStage.show();
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다