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 pathAddNewRowDemo.java
More file actions
145 lines (124 loc) · 4.79 KB
/
AddNewRowDemo.java
File metadata and controls
145 lines (124 loc) · 4.79 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
133
134
135
136
137
138
139
140
141
142
143
144
145
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.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class AddNewRowDemo 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);
FlowPane flowPane = new FlowPane(3, 3);
TextField tfCountry = new TextField();
TextField tfCapital = new TextField();
TextField tfPopulation = new TextField();
CheckBox chkDemocratic = new CheckBox("Is democratic?");
Button btAddRow = new Button("Add new row");
tfCountry.setPrefColumnCount(5);
tfCapital.setPrefColumnCount(5);
tfPopulation.setPrefColumnCount(5);
flowPane.getChildren().addAll(new Label("Country: "),
tfCountry, new Label("Capital"), tfCapital,
new Label("Population"), tfPopulation, chkDemocratic,
btAddRow);
btAddRow.setOnAction(e -> {
data.add(new Country(tfCountry.getText(), tfCapital.getText(),
Double.parseDouble(tfPopulation.getText()),
chkDemocratic.isSelected()));
tfCountry.clear();
tfCapital.clear();
tfPopulation.clear();
});
BorderPane pane = new BorderPane();
pane.setCenter(tableView);
pane.setBottom(flowPane);
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);
}
}