This repository was archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCellEditDemo.java
More file actions
132 lines (112 loc) · 4.29 KB
/
CellEditDemo.java
File metadata and controls
132 lines (112 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class CellEditDemo extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
TableView<Country> tableView = new TableView<>();
ObservableList<Country> data =
FXCollections.observableArrayList(
new Country("USA", "Washington DC", 280, true),
new Country("Canada", "Ottawa", 32, true),
new Country("United Kingdom", "London", 60, true),
new Country("Germany", "Berlin", 83, true),
new Country("France", "Paris", 60, true));
tableView.setItems(data);
TableColumn countryColumn = new TableColumn("Country");
countryColumn.setMinWidth(100);
countryColumn.setCellValueFactory(
new PropertyValueFactory<Country, String>("country"));
TableColumn capitalColumn = new TableColumn("Capital");
capitalColumn.setMinWidth(100);
capitalColumn.setCellValueFactory(
new PropertyValueFactory<Country, String>("capital"));
TableColumn populationColumn =
new TableColumn("Population (million)");
populationColumn.setMinWidth(100);
populationColumn.setCellValueFactory(
new PropertyValueFactory<Country, Double>("population"));
TableColumn democraticColumn =
new TableColumn("Is Democratic?");
democraticColumn.setMinWidth(100);
democraticColumn.setCellValueFactory(
new PropertyValueFactory<Country, Boolean>("democratic"));
tableView.getColumns().addAll(countryColumn, capitalColumn,
populationColumn, democraticColumn);
tableView.setEditable(true);
countryColumn.setCellFactory(TextFieldTableCell.forTableColumn());
countryColumn.setOnEditCommit(
new EventHandler<CellEditEvent<Country, String>>() {
@Override
public void handle(CellEditEvent<Country, String> t) {
t.getTableView().getItems().get(
t.getTablePosition().getRow())
.setCountry(t.getNewValue());
}
}
);
BorderPane pane = new BorderPane();
pane.setCenter(tableView);
Scene scene = new Scene(pane, 500, 250);
primaryStage.setTitle("AddNewRowDemo"); // Set the window title
primaryStage.setScene(scene); // Place the scene in the window
primaryStage.show(); // Display the window
}
public static class Country {
private final SimpleStringProperty country;
private final SimpleStringProperty capital;
private final SimpleDoubleProperty population;
private final SimpleBooleanProperty democratic;
private Country(String country, String capital,
double population, boolean democratic) {
this.country = new SimpleStringProperty(country);
this.capital = new SimpleStringProperty(capital);
this.population = new SimpleDoubleProperty(population);
this.democratic = new SimpleBooleanProperty(democratic);
}
public String getCountry() {
return country.get();
}
public void setCountry(String country) {
this.country.set(country);
}
public String getCapital() {
return capital.get();
}
public void setCapital(String capital) {
this.capital.set(capital);
}
public double getPopulation() {
return population.get();
}
public void setPopulation(double population) {
this.population.set(population);
}
public boolean isDemocratic() {
return democratic.get();
}
public void setDemocratic(boolean democratic) {
this.democratic.set(democratic);
}
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
* line.
*/
public static void main(String[] args) {
launch(args);
}
}