我为我的工作创建项目。我需要使用tableview和用户需要检查进入数据库的数据。 我发现如何从这篇文章中向TableView添加复选框 https://stackoverflow.com/a/7973514/3037869 这对我有用,但我的TableView现在正在寻找
如何修复此复选框垃圾邮件? 我的代码
public class ProductPSController extends BorderPane{
@FXML public TableColumn<ProductPS, String> produkt;
@FXML public TableColumn<ProductPS, String> symbol;
@FXML public TableColumn<ProductPS, String> atrybuty;
@FXML public TableColumn<ProductPS, Integer> id;
@FXML public TableColumn<ProductPS, Integer> stock;
@FXML public TableColumn<ProductPS, Integer> count;
@FXML public TableColumn<ProductPS, Integer> price;
@FXML public TableColumn<ProductPS, Boolean> checkbox;
@FXML public TableView <ProductPS> tab;
@FXML public BorderPane produkty;
public ObservableList<ProductPS> data = FXCollections.observableArrayList();
public ProductPSController()
{
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("product.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
id.prefWidthProperty().bind(tab.widthProperty().multiply(0.10));
stock.prefWidthProperty().bind(tab.widthProperty().multiply(0.10));
produkt.prefWidthProperty().bind(tab.widthProperty().multiply(0.20));
symbol.prefWidthProperty().bind(tab.widthProperty().multiply(0.10));
atrybuty.prefWidthProperty().bind(tab.widthProperty().multiply(0.30));
count.prefWidthProperty().bind(tab.widthProperty().multiply(0.10));
price.prefWidthProperty().bind(tab.widthProperty().multiply(0.10));
tab.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
setProduct();
}
public void setProduct()
{
id.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("id"));
symbol.setCellValueFactory(new PropertyValueFactory<ProductPS, String>("symbol"));
stock.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("id_stock"));
produkt.setCellValueFactory(new PropertyValueFactory<ProductPS, String>("product_name"));
atrybuty.setCellValueFactory(new PropertyValueFactory<ProductPS, String>("attributes"));
count.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("count"));
Callback<TableColumn<ProductPS, Boolean>, TableCell<ProductPS, Boolean>> booleanCellFactory =
new Callback<TableColumn<ProductPS, Boolean>, TableCell<ProductPS, Boolean>>() {
@Override
public TableCell<ProductPS, Boolean> call(TableColumn<ProductPS, Boolean> p) {
return new BooleanCell();
}
};
price.setCellValueFactory(new PropertyValueFactory<ProductPS, Integer>("price"));
checkbox.setCellValueFactory(new PropertyValueFactory<ProductPS, Boolean>("checkbox"));
checkbox.setCellFactory(booleanCellFactory);
checkbox.setEditable(true);
try(Connection c = MysqlConnect.getConnection())
{
String SQL = "";
ResultSet rs = c.createStatement().executeQuery(SQL);
while(rs.next()){
data.add(new ProductPS(rs.getInt("id_product"),rs.getInt("id_stock_available"),rs.getString("name"),rs.getString("atrybuty"),rs.getInt("quantity"),rs.getFloat("price"),rs.getString("reference")));
}
c.close();
} catch (SQLException e) {
System.out.println(e.toString());
// TODO Auto-generated catch block
}
for(int i=0; i<data.size(); i++) {
if(i+1<data.size() && data.get(i).getAttributes().length()==0 && data.get(i).getId()==data.get(i+1).getId() ){data.remove(i);}
}
tab.setItems(data);
}
class BooleanCell extends TableCell<ProductPS, Boolean> {
private CheckBox checkBox;
public BooleanCell() {
checkBox = new CheckBox();
checkBox.setDisable(false);
checkBox.selectedProperty().addListener(new ChangeListener<Boolean> () {
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
if(isEditing())
{
commitEdit(newValue == null ? false : newValue);
}
}
});
this.setGraphic(checkBox);
this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
this.setEditable(true);
}
@Override
public void startEdit() {
super.startEdit();
if (isEmpty()) {
return;
}
checkBox.setDisable(false);
checkBox.requestFocus();
}
@Override
public void cancelEdit() {
super.cancelEdit();
checkBox.setDisable(true);
}
public void commitEdit(Boolean value) {
super.commitEdit(value);
checkBox.setDisable(true);
}
@Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (!isEmpty()) {
checkBox.setSelected(item);
}
}
}
}
请求帮助,说出我做得不好的事。
答案 0 :(得分:1)
在BooleanCell
中,如果单元格不为空,则需要将图形设置为checkBox
,如果单元格为空,则需要将其设置为null
。
删除行
this.setGraphic(checkBox);
来自BooleanCell
的构造函数,并将updateItem(...)
更改为:
@Override
public void updateItem(Boolean item, boolean empty) {
super.updateItem(item, empty);
if (empty) {
setGraphic(null);
} else {
checkBox.setSelected(item);
setGraphic(checkBox);
}
}