Using ctrl + shift + p select Maven: New Project and Select an archetype: maven-archetype-quickstart
mvn archetype:generate -DgroupId=ceb.net.vn -DartifactId=cms-javafx -Dpackage=ceb -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>25</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>25</version>
</dependency>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>ceb.App</mainClass>
</configuration>
</plugin>
mvn javafx:run
Create views/category.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.control.*?>
<VBox xmlns:fx="http://javafx.com/fxml" fx:controller="ceb.controllers.CategoryController" spacing="10" padding="10">
<HBox spacing="10">
<Label text="Name" />
<TextField fx:id="txtName" prefWidth="400" />
</HBox>
<HBox spacing="10">
<Button text="Add" onAction="#onAdd" />
</HBox>
<TableView fx:id="tableView" prefHeight="250">
<columns>
<TableColumn fx:id="colId" text="Id" prefWidth="50" />
</columns>
</TableView>
</VBox>
Create controllers/CategoryController.java in folder controllers
public class CategoryController {
@FXML private TextField txtName;
@FXML private Button btnAdd;
@FXML private TableView<Category> tableView;
@FXML private TableColumn<Category, Short> colId;
private ObservableList<Category> categories;
@FXML
public void initialize(){
categories = FXCollections.observableList();
colId.setCellValueFactory(new PropertyValueFactory<Category, Short>("id"));
tableView.setItems(categories);
}
@FXML
void onAdd(){
}
}
public class App extends Application{
public static void main( String[] args ){
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("views/category.fxml"));
Scene scene = new Scene(loader.load(), 800, 600);
primaryStage.setTitle("CMS");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Create class Category for add
public class CategoryRepository {
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
private static Connection getConnection() throws SQLException{
return DriverManager.getConnection("jdbc:sqlserver://localhost:1433;Database=CMS;trustServerCertificate=true;", "sa", "123");
}
public int add(Category obj){
Connection connection = null;
CallableStatement stmt = null;
ResultSet rs = null;
try {
connection = getConnection();
stmt = connection.prepareCall("{CALL AddCategory(?, ?, ?)}");
stmt.registerOutParameter(1, Types.SMALLINT);
stmt.setString(2, obj.getName());
stmt.setString(3, obj.getDescription());
int ret = stmt.executeUpdate();
if(ret > 0) obj.setId(stmt.getShort(1));
return ret;
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}