问题
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