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 ObservableList<String[]> data = FXCollections.observableArrayList();
47 data.addAll(Arrays.asList(tiedot));
48 TableView<String[]> tableTiedot =
new TableView<>();
50 tableTiedot.setEditable(
true);
52 for (
int i = 0;
i < otsikot.length;
i++) {
53 TableColumn<String[], String> tc =
new TableColumn<String[], String>(otsikot[
i]);
54 final int sarakeNro =
i;
56 tc.setCellValueFactory((rivi) ->
new SimpleStringProperty(rivi.getValue()[sarakeNro]));
59 tableTiedot.getColumns().add(tc);
60 tc.setCellFactory(TextFieldTableCell.forTableColumn());
62 tc.setOnEditCommit( (
t) -> {
63 String[] rivi =
t.getTableView().getItems().get(
t.getTablePosition().getRow());
64 rivi[sarakeNro] =
t.getNewValue();
68 tableTiedot.setItems(data);
69 root.getChildren().add(tableTiedot);
70 primaryStage.setScene(
new Scene(root, 300, 250));