How to get data from Tab in Tabpane JavaFX

那年仲夏 提交于 2020-01-25 07:25:06

问题


I want get data from Tab in Tabpane JavaFX

I have 2 Tab in Tabpane, And each Tab I have a TextArea, I want click Button will get data from 2 tab

Here's my code:

btnThem.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        try {
            i++;
            FXMLLoader fxmlLoader = new FXMLLoader(
                    getClass().getResource("/fxml/tab.fxml"));
            Parent parent = (Parent) fxmlLoader.load();
            Tab tab = new Tab("Điểm " + i);
            tab.setContent(parent);
            tab.setClosable(true);
            tabPane.getTabs().add(tab);
            controllerTab = (ControllerTab) fxmlLoader.getController();

        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }
});

回答1:


Your question is ambiguous but it seems you want to get data from each textArea in tab.To do this you should get nodes (children) from each tab and by using lookup() we confirm and we parse node to textArea.I tried to figure your scene and made this example to help you :

public class example extends Application {

        TextArea textArea = new TextArea();
        TextArea textArea1 = new TextArea();
        Button button = new Button("button");

        @Override
        public void start(Stage primaryStage) {
            Tab tab1 = new Tab("tab1");
            Tab tab2 = new Tab("tab2");
            tab1.setContent(textArea);
            tab2.setContent(textArea1);
            TabPane pane = new TabPane();
            pane.getTabs().addAll(tab1, tab2);
            Node node1 = tab1.getContent();
            Node node2 = tab2.getContent();

            button.setOnAction((ActionEvent event) -> {
                if (node1.lookup("TextArea") != null && node2.lookup("TextArea") != null) {
                    TextArea area1 = (TextArea) node1.lookup("TextArea");
                    TextArea area2 = (TextArea) node2.lookup("TextArea");

                    System.out.println(area1.getText() + "   " + area2.getText());

                }
            });

            VBox root = new VBox();
            root.setAlignment(Pos.TOP_RIGHT);
            root.getChildren().add(pane);
            root.getChildren().add(button);

            Scene scene = new Scene(root, 300, 250);
            primaryStage.setTitle("Hello World!");
            primaryStage.setScene(scene);
            primaryStage.show();
        }

        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            launch(args);
        }

    }

And you can see the result :

Hello ,tab1   Hello ,tab2
Deleting directory C:\Users\Electron\Documents\NetBeansProjects\Buttono\dist\run341573612
jfxsa-run:
BUILD SUCCESSFUL (total time: 22 seconds)


来源:https://stackoverflow.com/questions/47897569/how-to-get-data-from-tab-in-tabpane-javafx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!