Parte2 Projeto Javafx JDBC
Parte2 Projeto Javafx JDBC
educandoweb.com
Prof. Dr. Nelio Alves
Objetivo geral:
PROJETO: https://github.com/acenelio/workshop-javafx-jdbc
Github project
Checklist:
Gitignore: Java
Checklist:
User libraries: JavaFX, MySQLConnector
Run configurarions -> VM arguments:
--module-path C:\java-libs\javafx-sdk\lib --add-modules=javafx.fxml,javafx.controls
Git:
o git init
o git remote add origin https://github.com/acenelio/workshop-javafx-jdbc.git
o git pull origin master
Gitignore:
# build folders
bin/
target/
nbproject/private/
build/
nbbuild/
dist/
nbdist/
Main view
Checklist:
Create FXML "MainView" (package "gui")
Load FXML in Main
Update Main.java
@Override
public void start(Stage primaryStage) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/gui/MainView.fxml"));
Parent parent = loader.load();
Checklist:
Design MainView.fxml
Customize menu items
Update Main.java
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.VBox?>
Checklist:
Create controller
In view, associate controller, ids, events
About view
Checklist:
Include util classes to the project (Alerts.java, Constraints.java)
https://github.com/acenelio/javafx5/blob/master/src/gui/util/Alerts.java
https://github.com/acenelio/javafx5/blob/master/src/gui/util/Constraints.java
Create About.fxml (VBox)
In Main.java, expose mainScene reference
In MainViewController.java, create loadView method
Checklist:
Create DepartmentList.fxml (VBox)
In MainViewController.java, load DepartmentList
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.ToolBar?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
Checklist:
Create model.entities.Department.java
https://github.com/acenelio/demo-dao-jdbc/blob/master/src/model/entities/Department.java
Create DepartmentListController.java
In view, associate controller, ids, events
DepartmentService
Checklist:
Create model.services.DepartmentService.java with findAll method
In DepartmentListController:
o Create DepartmentService dependency with set method
o Create ObservableList<Department>
o Create updateTableViewData method
Checklist:
Add a Consumer<T> parameter to loadView method
After loading view, call accept from the Consumer
Add a consumer instance on loadView calls
Prerequisites:
MySQL server installed and running
Database created and instantiated
https://github.com/acenelio/demo-dao-jdbc/blob/master/database.sql
Data access layer implemented (DAO pattern):
https://github.com/acenelio/demo-dao-jdbc
Checklist:
Add model.entities.Seller.java
Add db.properties do project
Add data access packages to project:
o db
o model.dao
o model.dao.impl
In DepartmentService, add DepartmentDao dependency with Factory call
DepartmentForm (dialog) design
Checklist:
Create gui.util.Utils.java with currentStage method
Create DepartmentForm.fxml (AnchorPane)
o GridPane 3x3 (anchors: 20 top, 20 left)
o Id text box: not editable
o Label error: red
o HBox (spacing: 5)
In DepartmentListController, create createDialogForm method
Call createDialogForm on "new" button action
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.RowConstraints?>
Checklist:
Create DepartmentFormController.java
In view, associate controller, ids, events
Checklist:
In DepartmentFormController
o Create a Department dependency with set method
o Create updateFormData method
In DepartmentListController
o Update onBtNewAction method
o Update createDialogForm method
Checklist:
In Utils, implement tryParseToInt method
In DepartmentService, create saveOrUpdate method
In DepartmentFormController
o Create a DepartmentService dependency with set method
o Implement onBtSaveAction
o Implement onBtCancelAction
In DepartmentListController, inject DepartmentService instance
Checklist:
Create interface gui.listeners.DataChangeListener
In DepartmentFormController (subject)
o Create List<DataChangeListener> dependency with subscribe method
o Notify subscribers when needed
In DepartmentListController (observer)
o Implement DataChangeListener interface
o Subscribe for DepartmentFormController
Validation exception
Checklist:
Create model.exceptions.ValidationException
In DepartmentFormController
o In getFormData method, implement verifications and throw ValidationException
o Implement setErrorMessages method
o In onBtSaveAction, catch ValidationException
Update department
References:
https://stackoverflow.com/questions/32282230/fxml-javafx-8-tableview-make-a-delete-button-in-each-row-and-
delete-the-row-a
Checklist:
In DepartmentListController
o Create new attribute: TableColumn<Department, Department> tableColumnEDIT;
o Create initEditButtons method
o In updateTableViewData, call initEditButtons
In DepartmentList.fxml
o Include new table column
o Associate id
@Override
protected void updateItem(Department obj, boolean empty) {
super.updateItem(obj, empty);
if (obj == null) {
setGraphic(null);
return;
}
setGraphic(button);
button.setOnAction(
event -> createDialogForm(
obj, "/gui/DepartmentForm.fxml",Utils.currentStage(event)));
}
});
}
Remove department
References:
https://stackoverflow.com/questions/32282230/fxml-javafx-8-tableview-make-a-delete-button-in-each-row-and-
delete-the-row-a
Checklist:
In Alerts, create showConfirmation method
In DepartmentService, create remove method
In DepartmentListController
o Create new attribute: TableColumn<Department, Department> tableColumnREMOVE;
o Create initRemoveButtons method
Catch DbIntegrityException
o In updateTableViewData, call initRemoveButtons
In DepartmentList.fxml
o Include new table column
o Associate id
public static Optional<ButtonType> showConfirmation(String title, String content) {
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(content);
return alert.showAndWait();
}
@Override
protected void updateItem(Department obj, boolean empty) {
super.updateItem(obj, empty);
if (obj == null) {
setGraphic(null);
return;
}
setGraphic(button);
button.setOnAction(event -> removeEntity(obj));
}
});
}
Checklist:
.gitignore: .settings/
Delete .settings/ folder
SellerList
Checklist:
Clone SellerService
o Replace: Department -> Seller
Clone SellerListController
o Replace: Department -> Seller
o Comment createDialogForm
Clone SellerList.fxml
o Replace: Department -> Seller
Update MainViewController.onMenuItemSellerAction
Seller TableView
References:
https://stackoverflow.com/questions/47484280/format-of-date-in-the-javafx-tableview
Checklist:
gui.utils.Util.java
o formatTableColumnDate method
o formatTableColumnDouble method
SellerListController
o TableColumn attributes (Email, BirthDate, BaseSalary)
o Update initializeNodes
SellerListView
o TableColumn (Email, BirthDate, BaseSalary)
o Associate fx:id
@Override
protected void updateItem(Date item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
setText(sdf.format(item));
}
}
};
return cell;
});
}
@Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setText(null);
} else {
Locale.setDefault(Locale.US);
setText(String.format("%."+decimalPlaces+"f", item));
}
}
};
return cell;
});
}
SellerForm
Checklist:
Clone SellerFormController
o Replace: Department -> Seller
Clone SellerForm view
o Replace: Department -> Seller
SellerListController
o Uncomment createDialogForm
References:
https://stackoverflow.com/questions/26831978/javafx-datepicker-getvalue-in-a-specific-format
Checklist:
gui.utils.Util.java
o formatDatePicker method
TextField & DatePicker attributes (Email, BirthDate, BaseSalary)
Label error attributes (Email, BirthDate, BaseSalary)
Edit SellerFormView
Bugfix: SellerDaoJDBC.instantiateSeller
obj.setBirthDate(new java.util.Date(rs.getTimestamp("BirthDate").getTime()));
Update: initializeNodes
Update: updateFormData
{
datePicker.setPromptText(format.toLowerCase());
}
@Override
public String toString(LocalDate date) {
if (date != null) {
return dateFormatter.format(date);
} else {
return "";
}
}
@Override
public LocalDate fromString(String string) {
if (string != null && !string.isEmpty()) {
return LocalDate.parse(string, dateFormatter);
} else {
return null;
}
}
});
}
Department ComboBox
Checklist:
Update controller:
o DepartmentService dependency
attribute
set method
o ComboBox<Department> comboBoxDepartment
o ObservableList<Department> obsList
loadAssociatedObjects
o initializeComboBoxDepartment
o updateFormData
Update view:
o ComboBox
o fx:id
comboBoxDepartment.setCellFactory(factory);
comboBoxDepartment.setButtonCell(factory.call(null));
}
Saving Seller
Checklist:
Update: getFormData
Update: setErrorMessages
Checklist:
Build JAR file
o Right click project name -> Export -> Java/Runnable JAR file -> Next
o Select Main class
o Select destination folder
o Library handling: 3rd option
Pack files
o JAR file
o db.properties
o MySQL Connector
o JavaFX SDK
o Java JDK
Instalation
Checklist:
Install Java: https://www.oracle.com/technetwork/java/javase/downloads/index.html
o Setup JAVA_HOME (ex: C:\Program Files\Java\jdk-17.0.3)
Copy JavaFX
o Setup PATH_TO_FX (ex: C:\java-libs\javafx-sdk\lib)
o Place MySQL Connector in lib folder
Copy JAR & db.properties
Run app:
java --module-path %PATH_TO_FX% --add-modules javafx.controls,javafx.fxml -cp myapp.jar
application.Main
Target:
"C:\Program Files\Java\jdk-17.0.3\bin\java.exe" --module-path %PATH_TO_FX% --add-modules
javafx.controls,javafx.fxml -cp myapp.jar application.Main
Start in:
C:\appfolder