The best way to get from user two dimensional array in javaFX

吃可爱长大的小学妹 提交于 2020-01-06 13:51:47

问题


I need to get matrix from user. Which of the JavaFX features is the most useful? Grid with (m*n) textFields or TableView? The thing is, it is hard to represent TableView with variable number of columns.


回答1:


I'd use a TextArea

public class Matrix extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextArea txt = new TextArea();

        GridPane grid = new GridPane();
        grid.setGridLinesVisible(true);

        txt.setOnKeyReleased(t-> {
            int i = 0;
            String[] rows = txt.getText().split("\n");
            for (String row: rows){
                String[] cols = row.split("\\s+");
                int j = 0;
                for (String col : cols)
                    grid.add(new Text(col), j++, i);
                i++;
            }
        });


        VBox root = new VBox(txt, grid);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

I'd probably do some better formatting:) You could also use a TextField but split with brackets or something.



来源:https://stackoverflow.com/questions/30223916/the-best-way-to-get-from-user-two-dimensional-array-in-javafx

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