JavaFX:如何通过引用textarea来选择textarea所在的选项卡?

JavaFX:如何通过引用textarea来选择textarea所在的选项卡?,javafx,Javafx,JavaFX:如何通过引用textarea来选择textarea所在的选项卡 任何帮助都将不胜感激,谢谢 您可以遍历节点层次结构,直到找到一个节点,其中一个节点的样式类为选项卡内容区域。这是选项卡的内容节点。根据此信息,您可以找到相应的选项卡: public static Tab findTab(Node node, TabPane tabPane) { if (tabPane == null) { throw new IllegalArgumentException()

JavaFX:如何通过引用textarea来选择textarea所在的选项卡


任何帮助都将不胜感激,谢谢

您可以遍历节点层次结构,直到找到一个
节点
,其中一个节点的样式类为
选项卡内容区域
。这是
选项卡的内容节点。根据此信息,您可以找到相应的
选项卡

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();
}
公共静态选项卡findTab(节点节点,选项卡窗格){
if(tabPane==null){
抛出新的IllegalArgumentException();
}
//查找包含节点的内容节点
Node parent=Node.getParent();
而(parent!=null&&!parent.getStyleClass()包含(“选项卡内容区域”)){
节点=父节点;
parent=node.getParent();
}
//在到达选项卡内容之前已到达根目录
如果(父项==null){
返回null;
}
//找到合适的标签
for(选项卡:tabPane.getTabs()){
if(tab.getContent()==节点){
返回选项卡;
}
}
返回null;
}
@凌驾
公共无效开始(阶段primaryStage){
TabPane TabPane=新建TabPane();
TextArea textArea1=新的TextArea();
TextArea textArea2=新TextArea();
Tab tab1=新选项卡(“tab1”,文本区域1);
//包装一些父项以显示此功能也正常工作
Tab tab2=新选项卡(“tab2”,新堆栈窗格(textArea2));
tabPane.getTabs().addAll(tab1,tab2);
ChangeListener文本ChangeListener=(可观察、旧值、新值)->{
//获取包含属性的节点
Node sourceNode=(Node)((ReadOnlyProperty)可观察);
//找到相应的选项卡并选择它
Tab Tab=findTab(sourceNode,tabPane);
如果(制表符!=null){
tabPane.getSelectionModel().select(选项卡);
}
};
textArea1.textProperty().addListener(textChangeListener);
textArea2.textProperty().addListener(textChangeListener);
//交替向文本区域添加一些文本
Timeline Timeline=新的时间线(新的关键帧(持续时间.秒(2),新的EventHandler()){
私有int num=1;
@凌驾
公共无效句柄(ActionEvent事件){
textArea1.appendText(“\n”+num);
num+=2;
}
}),新的关键帧(Duration.seconds(4),新的EventHandler(){
私有整数;
@凌驾
公共无效句柄(ActionEvent事件){
textArea2.appendText(“\n”+num);
num+=2;
}
}));
timeline.setCycleCount(Animation.unfinite);
timeline.play();
场景=新场景(选项卡窗格);
初级阶段。场景(场景);
primaryStage.show();
}

您可以遍历节点层次结构,直到找到一个
节点,该节点的父节点为样式类
选项卡内容区
。这是
选项卡的内容节点。根据此信息,您可以找到相应的
选项卡

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();
}
公共静态选项卡findTab(节点节点,选项卡窗格){
if(tabPane==null){
抛出新的IllegalArgumentException();
}
//查找包含节点的内容节点
Node parent=Node.getParent();
而(parent!=null&&!parent.getStyleClass()包含(“选项卡内容区域”)){
节点=父节点;
parent=node.getParent();
}
//在到达选项卡内容之前已到达根目录
如果(父项==null){
返回null;
}
//找到合适的标签
for(选项卡:tabPane.getTabs()){
if(tab.getContent()==节点){
返回选项卡;
}
}
返回null;
}
@凌驾
公共无效开始(阶段primaryStage){
TabPane TabPane=新建TabPane();
TextArea textArea1=新的TextArea();
TextArea textArea2=新TextArea();
Tab tab1=新选项卡(“tab1”,文本区域1);
//包装一些父项以显示此功能也正常工作
Tab tab2=新选项卡(“tab2”,新堆栈窗格(textArea2));
tabPane.getTabs().addAll(tab1,tab2);
ChangeListener文本ChangeListener=(可观察、旧值、新值)->{
//获取包含属性的节点
Node sourceNode=(Node)((ReadOnlyProperty)可观察);
//找到相应的选项卡并选择它
Tab Tab=findTab(sourceNode,tabPane);
如果(制表符!=null){
tabPane.getSelectionModel().select(选项卡);
}
};
textArea1.textProperty().addListener(textChangeListener);
textArea2.textProperty().addListener(textChangeListener);
//交替向文本区域添加一些文本
Timeline Timeline=新的时间线(新的关键帧(持续时间.秒(2),新的EventHandler()){
私有int num=1;
@凌驾
公共无效句柄(ActionEvent事件){
textArea1.appendText(“\n”+num);
num+=2;
}
}),新的关键帧(Duration.seconds(4),新的EventHandler(){
私有整数;
@凌驾
公共无效句柄(ActionEvent事件){
textArea2.appendText(“\n”+num);
num+=2;
}
}));
timeline.setCycleCount(Animation.unfinite);
timeline.play();
场景=新场景(选项卡窗格);
初级阶段。场景(场景);
primaryStage.show();
}