androizer
androizer

Reputation: 97

JavaFX : Dynamic cell control on button click in Tableview?

I've been working with Table View in Javafx, and I want to dynamically control the cell on a button click which itself is too part of Table View. I tried it to do it myself, but its not working as it should be. So help is needed as em not able to do it.

Button TableCell code:

 dlColumn.setCellValueFactory(new PropertyValueFactory<>(""));
 dlColumn.setCellFactory(new Callback<TableColumn<SharedFileInfo, String>, TableCell<SharedFileInfo, String>>() {

        @Override
        public TableCell<SharedFileInfo, String> call(TableColumn<SharedFileInfo, String> param) {

            final TableCell<SharedFileInfo, String> cell;
            cell = new TableCell<SharedFileInfo, String>() {
                final Button downloadButton = new Button();

                @Override
                protected void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty);
                    if (empty) {
                        setGraphic(null);
                        setText(null);
                    } else {
                        downloadButton.setOnAction((ActionEvent ae) -> {
                             progressColumn.setCellValueFactory(new PropertyValueFactory<>("progress"));
                             progressColumn.setCellFactory(new Callback<TableColumn<SharedFileInfo, Double>, TableCell<SharedFileInfo, Double>>() {

                        @Override
                        public TableCell<SharedFileInfo, Double> call(TableColumn<SharedFileInfo, Double> param) {

                        final ProgressBar progressBar = new ProgressBar();

                        final TableCell<SharedFileInfo, Double> cell;
                        cell = new TableCell<SharedFileInfo, Double>() {

                              @Override
                              protected void updateItem(Double item, boolean empty) {
                              super.updateItem(item, true);
                              if (empty) {
                                 setGraphic(null);
                                 setText(null);
                              } else {
                                  SharedFileInfo sharedinfo = getTableView().getItems().get(this.getIndex());
                                  progressBar.progressProperty().bind(sharedinfo.getProgress());

                                  setGraphic(progressBar);
                                  progressBar.prefWidthProperty().bind(this.widthProperty());
                                  setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                                   }
                              }

                           };
                           return cell;
                       }
                   });
                        });
                        setGraphic(downloadButton);
                        setText(null);
                    }
                }
            };
            return cell;
        }
    });


Before clicking button, all seems to work as expected, but when I click the button, the whole progress column get filled with empty Progress Bars which is what I don't want to. I want, whenever a user clicks button, only that row shows the Progress Bar and all rest sets to setGraphic(null);. Please help me with this.!
enter image description here> enter image description here

Upvotes: 0

Views: 1674

Answers (1)

Manuel Seiche
Manuel Seiche

Reputation: 221

Currently I'm not able to access my IDE, so I'll try to write some plain text.

On a click of (any) Download-Button, you assign a new CellFactory to the whole column, which doesn't distinguish between the row you want to be "progressed" and all others. All of your Records refer to their progressProperty, but only the one progressProperty of your clicked SharedFileInfo-Row is changing.

If you want to keep doing it like this adding a CellFactory on every ButtonClick,
(which I would not suggest, because you could also set it once and implement a trigger BooleanProperty for example) you could check, if the SharedFileInfo-instance of the clicked Buttons row is == the SharedFileInfo-instance in your inner CellFactory

Hint: Javafx, get the object referenced by a TableCell

Edit: One example how to reach your Goal would work like this:

  1. Create a global SharedFileInfo field
  2. Set the CellFactories at the beginning
  3. Button-Column-Factory: On Button-Click set the global field = the clickedCells SharedFileInfo-instance
  4. Progress-Column-Factory: Each cell checks, whether its SharedFileInfo-instance is == the value of your global field and if so, shows its progress-bar

Note that you probably have to "tell" your cells to update their appearance after setting the value of the global field. I suggest to work with a global Property+ChangeListener.

Upvotes: 2

Related Questions