Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (3)
......@@ -11,4 +11,7 @@
*.ear
*.iml
*.idea
\ No newline at end of file
*.idea
/java1/
derby.log
\ No newline at end of file
......@@ -8,39 +8,40 @@
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<JavaFX.version>22-ea+16</JavaFX.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>14</version>
<version>${JavaFX.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>14</version>
<version>${JavaFX.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.5.2</version>
<version>5.10.1</version>
<scope>test</scope>
</dependency>
......@@ -48,26 +49,26 @@
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.18</version>
<version>42.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle.database.jdbc/ojdbc10 -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc10</artifactId>
<version>19.8.0.0</version>
<version>19.21.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.derby/derby -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version>
<version>10.17.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.derby/derbyclient -->
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.15.2.0</version>
<version>10.17.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.derby/derbytools -->
<dependency>
......
......@@ -6,6 +6,7 @@ import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
......@@ -16,80 +17,98 @@ import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
public class TableViewSample extends Application {
private final PersonDAO dao;
private TableView<Person> table = new TableView<>();
private final ObservableList<Person> data;
final HBox hb = new HBox();
private final ObservableList<Person> data;
final GridPane gridPane = new GridPane();
private TextField addFirstName;
private TextField addLastName;
private TextField addEmail;
public static void main(String[] args) {
launch(args);
}
public TableViewSample() {
dao = new PersonDAO(new DerbyJDBCDialect());
data = FXCollections.observableArrayList(dao.getAll());
public static void main(String[] args) {
launch(args);
}
@SuppressWarnings("unchecked")
public TableViewSample() {
dao = new PersonDAO(new DerbyJDBCDialect());
data = FXCollections.observableArrayList(dao.getAll());
}
@SuppressWarnings("unchecked")
@Override
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(550);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
table.setItems(data);
TableColumn<Person, String> firstNameCol = constructColumn("First Name","firstName", this::handleOnEditCommitFirstName);
TableColumn<Person,String> lastNameCol = constructColumn("Last Name","lastName", this::handleOnEditCommitLastName);
TableColumn<Person,String> emailCol = constructColumn("Email","email",this::handleOnEditCommitEmail);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
addFirstName = constructTextField(firstNameCol,"First Name");
addLastName = constructTextField(lastNameCol,"Last Name");
addEmail = constructTextField(emailCol,"Email");
final Button addButton = new Button("Add");
addButton.setOnAction(this::handleOnAdd);
hb.getChildren().addAll(addFirstName, addLastName, addEmail, addButton);
hb.setSpacing(3);
final VBox vbox = new VBox();
vbox.setSpacing(5);
vbox.setPadding(new Insets(10, 0, 0, 10));
vbox.getChildren().addAll(label, table, hb);
((Group) scene.getRoot()).getChildren().addAll(vbox);
stage.setScene(scene);
stage.show();
stage.setOnCloseRequest(this::exitProgram);
}
private TextField constructTextField(TableColumn<Person, String> column, String string) {
public void start(Stage stage) {
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(450);
stage.setHeight(550);
final Label label = new Label("Address Book");
label.setFont(new Font("Arial", 20));
table.setEditable(true);
table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY_ALL_COLUMNS);
table.setItems(data);
TableColumn<Person, String> firstNameCol = constructColumn("First Name", "firstName",
this::handleOnEditCommitFirstName);
TableColumn<Person, String> lastNameCol = constructColumn("Last Name", "lastName",
this::handleOnEditCommitLastName);
TableColumn<Person, String> emailCol = constructColumn("Email", "email", this::handleOnEditCommitEmail);
table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
addFirstName = constructTextField("First Name");
addLastName = constructTextField("Last Name");
addEmail = constructTextField("Email");
final Button addButton = new Button("Add");
addButton.setMinWidth(Region.USE_PREF_SIZE);
addButton.setOnAction(this::handleOnAdd);
gridPane.add(addFirstName, 0, 0);
gridPane.add(addLastName, 1, 0);
gridPane.add(addEmail, 2, 0);
gridPane.add(addButton, 3, 0);
GridPane.setHgrow(addFirstName, Priority.ALWAYS);
GridPane.setHgrow(addLastName, Priority.ALWAYS);
GridPane.setHgrow(addEmail, Priority.ALWAYS);
GridPane.setHgrow(addButton, Priority.NEVER);
gridPane.setHgap(5);
GridPane.setFillWidth(addButton, true);
final BorderPane borderPane = new BorderPane();
borderPane.setTop(label);
BorderPane.setMargin(label, new Insets(10));
BorderPane.setAlignment(label, Pos.CENTER);
borderPane.setCenter(table);
BorderPane.setMargin(table, new Insets(10));
borderPane.setBottom(gridPane);
BorderPane.setMargin(gridPane, new Insets(10));
scene.setRoot(borderPane);
stage.setScene(scene);
stage.show();
stage.setOnCloseRequest(this::exitProgram);
}
private TextField constructTextField( String string) {
TextField result = new TextField();
result.setPromptText("First Name");
result.setMaxWidth(column.getPrefWidth());
return result;
result.setPromptText(string);
return result;
}
private TableColumn<Person, String> constructColumn(String name, String propertyName,
......@@ -110,24 +129,25 @@ public class TableViewSample extends Application {
addLastName.clear();
addEmail.clear();
}
private void handleOnEditCommitFirstName(CellEditEvent<Person, String> t) {
Person p = t.getTableView().getItems().get(t.getTablePosition().getRow());
p.setFirstName(t.getNewValue());
dao.updatePerson(p);
}
private void handleOnEditCommitLastName(CellEditEvent<Person, String> t) {
Person p = t.getTableView().getItems().get(t.getTablePosition().getRow());
p.setLastName(t.getNewValue());
dao.updatePerson(p);
}
private void handleOnEditCommitEmail(CellEditEvent<Person, String> t) {
Person p = t.getTableView().getItems().get(t.getTablePosition().getRow());
p.setEmail(t.getNewValue());
dao.updatePerson(p);
}
private void exitProgram(WindowEvent evt) {
try {
dao.close();
......