0

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

androizer
  • 77
  • 9

1 Answers1

2

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.

Community
  • 1
  • 1
Manuel Seiche
  • 231
  • 1
  • 5
  • I was not able to check my question after posting, so I think of so some other way which could do the desired task for me, and it's somehow based on ur idea. I haven't shown the whole table here, but it's interpretable that there will b a column for Name too which in every case is going to b static (created once, while rendering). So i explicitly provide the Cell Factory for it, and set `setProgress(2);` whenever Name cell is rendered. So, I could use this behavior of progress property to cross-check in Progress-Cell-Factory when it's == 2, `setGraphic(null)`, else `setGraphic(progressBar)`. – androizer Mar 31 '16 at 01:41
  • I'll wait for a day, to let other users to give their answers if there can be any other way around, else I'll mark ur answer as final. – androizer Mar 31 '16 at 01:48