JavaFX从Tab中获取元素

时间:2015-07-21 14:16:22

标签: java javafx tabs javafx-8

package sample.GUI;

import sample.sampleThing;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;

import java.net.URL;
import java.util.Objects;
import java.util.ResourceBundle;

@SuppressWarnings("unused")
public class Console  implements Initializable {
    public TextField messageOut = new TextField();
    public TextArea consoleOutput = new TextArea();
    public AnchorPane TabAnchorPane = new AnchorPane(messageOut, consoleOutput);
    public Tab tab = new Tab("Tab", TabAnchorPane);

    @FXML
    private TabPane tabPane;
    SingleSelectionModel<Tab> selectionModel = tabPane.getSelectionModel();

    String string1, string2;

    public Console() {
         super();
    }

    public Console(String str, String str2) {
        super();
        this.string1 = str;
        this.string2 = str2;
    }

    public void newTab(String str){
        Tab ntab = tab;
        tab.setText(str);
        tabPane.getTabs().add(ntab);
    }

    public void initialize(URL url, ResourceBundle rb){
        Tab tab1 = tab, tab2 = tab;
        tab1.setText(string1);
        tab2.setText(string2);
        tabPane.getTabs().addAll(tab1, tab2);
    }

    public void clear() {
        Tab tmptb = selectionModel.getSelectedItem();

      //GUIUtils.runSafe(tmptb.consoleOutput::clear);
    }

来自我的GUI类的代码(为了保护我的项目而重命名的内容) 我需要帮助的是获得consoleOutput&amp;&amp; messageOut每个标签 具体来说

node[] arr = getTab.getContent.getNodeArray //or something like that
for(node nd : arr) {
    if (nd == consoleOutput || nd == messageOut)
        //do much stuff
}

我可以提供更多信息,但我不知道应该提供什么 GUIUtils.runSafe只是从https://codereview.stackexchange.com/questions/52197/console-component-in-javafx获取的可运行的 实际上这个代码的一部分是基于他的......

1 个答案:

答案 0 :(得分:1)

Tab to Node,Node to Parent,父母通常有子女:

((Parent) tab.getContent()).
    getChildrenUnmodifiable();