1 package tableViewExamples;
4 import java.util.Arrays;
6 import javafx.application.Application;
7 import javafx.beans.property.SimpleStringProperty;
8 import javafx.collections.FXCollections;
9 import javafx.collections.ObservableList;
10 import javafx.scene.Scene;
11 import javafx.scene.control.TableColumn;
12 import javafx.scene.control.TableView;
13 import javafx.scene.control.cell.TextFieldTableCell;
14 import javafx.scene.layout.StackPane;
15 import javafx.stage.Stage;
31 public static void main(String[] args) {
37 public void start(Stage primaryStage) {
38 StackPane root =
new StackPane();
39 String[] otsikot = {
"ala",
"aloitusvuosi",
"h/vko"};
41 {
"kalastus",
"1955",
"20"},
42 {
"laiskottelu",
"1950",
"20"},
43 {
"työn pakoilu",
"1952",
"40"}
46 TableView<String[]> tableTiedot =
new TableView<>();
48 tableTiedot.setEditable(
true);
50 root.getChildren().add(tableTiedot);
51 primaryStage.setScene(
new Scene(root, 300, 250));
65 public static void NaytaTalukossa(TableView<String[]> tableTiedot, String[] otsikot, String[][] tiedot) {
66 ObservableList<String[]> data = FXCollections.observableArrayList();
67 data.addAll(Arrays.asList(tiedot));
68 for (
int i = 0;
i < otsikot.length;
i++) {
69 TableColumn<String[], String> tc =
new TableColumn<String[], String>(otsikot[
i]);
70 final int sarakeNro =
i;
72 tc.setCellValueFactory((rivi) ->
new SimpleStringProperty(rivi.getValue()[sarakeNro]));
75 tableTiedot.getColumns().add(tc);
76 tc.setCellFactory(TextFieldTableCell.forTableColumn());
78 tc.setOnEditCommit( (
t) -> {
79 String[] rivi =
t.getTableView().getItems().get(
t.getTablePosition().getRow());
80 rivi[sarakeNro] =
t.getNewValue();
84 tableTiedot.setItems(data);