Mình muốn khởi tạo 2 nút edit và delete trong table view
package controllers;
import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import dao.ProductDAO;
import entities.Product;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
public class MainController implements Initializable {
@FXML
private TableView<Product> table;
@FXML
private TableColumn<Product, Integer> idCol;
@FXML
private TableColumn<Product, String> nameCol;
@FXML
private TableColumn<Product, Float> priceCol;
@FXML
private TableColumn<Product, Integer> amountCol;
@FXML
private TableColumn<Product, Product> actionCol;
private ObservableList<Product> getProductList() throws SQLException {
ObservableList<Product> list = FXCollections.observableArrayList();
ProductDAO productDAO = new ProductDAO();
list.addAll(productDAO.findAll());
return list;
}
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
priceCol.setCellValueFactory(new PropertyValueFactory<>("price"));
amountCol.setCellValueFactory(new PropertyValueFactory<>("amount"));
actionCol.setCellFactory(param -> new TableCell<Product, Product>() {
private final Button editButton = new Button("edit");
private final Button deleteButton = new Button("delete");
@Override
public void updateItem(Product product, boolean item) {
super.updateItem(product, item);
if (product == null) {
setGraphic(null);
return;
}
deleteButton.setOnAction(event -> {
Product getProduct = getTableView().getItems().get(getIndex());
System.out.println(getProduct.getId() + " " + getProduct.getName());
});
editButton.setOnAction(event -> {
Product getProduct = getTableView().getItems().get(getIndex());
System.out.println(getProduct.getId());
});
HBox pane = new HBox(deleteButton, editButton);
setGraphic(pane);
}
});
ObservableList<Product> list = null;
try {
list = getProductList();
} catch (SQLException e) {
e.printStackTrace();
}
table.setItems(list);
}
}
nhưng khi chạy nó lại không hiển thị
File FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controllers.MainController">
<children>
<TabPane layoutY="-1.0" prefHeight="400.0" prefWidth="600.0" tabClosingPolicy="UNAVAILABLE">
<tabs>
<Tab text="Quản lý sản phẩm">
<content>
<TableView fx:id="table" prefHeight="200.0" prefWidth="200.0">
<columns>
<TableColumn fx:id="idCol" prefWidth="111.0" text="Mã sản phẩm" />
<TableColumn fx:id="nameCol" prefWidth="100.0" text="Tên sản phẩm" />
<TableColumn fx:id="priceCol" prefWidth="79.0" text="Giá tiền" />
<TableColumn fx:id="amountCol" prefWidth="78.0" text="Số lượng" />
<TableColumn fx:id="actionCol" minWidth="7.0" prefWidth="89.0" text="Hành động" />
</columns>
</TableView>
</content>
</Tab>
<Tab text="Quản lý đơn hàng">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
<Tab text="Quản lý tài khoản">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>