0

I creating project for my job. I need to use tableview and user need to check data going to database. I was found how to add checkbox to TableView from this post https://stackoverflow.com/a/7973514/3037869 This is working for me but my TableView now looking enter image description here

How to fix this checkbox spam? My code

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);
            }
        }
    }
}

Thx for help and say what i done bad.

Community
  • 1
  • 1
seti
  • 341
  • 1
  • 7
  • 19

1 Answers1

1

In BooleanCell, you need to set the graphic to checkBox if the cell is not empty, and set it to null if it is empty.

Remove the line

this.setGraphic(checkBox);

from BooleanCell's constructor, and change updateItem(...) to:

@Override
public void updateItem(Boolean item, boolean empty) {
    super.updateItem(item, empty);
    if (empty) {
        setGraphic(null);
    } else {
        checkBox.setSelected(item);
        setGraphic(checkBox);
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • After change updateItem i don't see any checkbox what is wrong? – seti Jul 11 '14 at 10:51
  • I checked var item and empty for all rows empty is true and item is null. Never come to else. Where is error? – seti Jul 11 '14 at 11:14
  • Found error bad name of set and get method. How to get row of table of checked checkbox? – seti Jul 11 '14 at 11:23