0

Is it possible to put text and a progressBar in the same cell?. I currently have string based cells, but I also want to add some cells that have a string and a progress bar. The following (bad) drawing is an example: bad drawing

I would like to do this as some items will show in the list before being ready to select (still creating the file corresponding to them). However some will be ready to select, so I am trying to avoid a separate Progress column as this would need to suddenly appear and disappear depending on if all items are ready. I also would like the items without progress bars to be String cells (but if this is not possible then I can improvise). Is this possible, and if not, any helpful suggestions on what i'm trying to achieve. Thanks :)

  • 2
    you need a custom TableCell and a custom object class (for the column) that has both the text and - have a look at the implementation of ProgressTableCell and go from there. When stuck in the process, please come back with a [mcve] that demonstrates how/why your code misbehaves – kleopatra Oct 08 '19 at 08:42
  • Look at this question, it is similar: [JavaFX Update progressbar in tableview from Task](https://stackoverflow.com/questions/16721380/javafx-update-progressbar-in-tableview-from-task). The solution demonstrates use of the `ProgressBarTableCell` which kleopatra recommended you take a look at. In that solution the text and progress bar are in different columns, perhaps you might want to consider a similar approach rather than the Progress bar and text in the same cell, though, with more effort on your part to implement it, the later would also work (to do that set the cell text and graphic). – jewelsea Oct 08 '19 at 21:23

1 Answers1

0

Code sample shows how to put any content into table column cell.

    import javafx.application.Application;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.beans.property.StringProperty;
    import javafx.beans.value.ChangeListener;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.scene.Scene;
    import javafx.scene.control.TableCell;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.TextField;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.Priority;
    import javafx.stage.Stage;

    public class TableViewApp extends Application {

        public static void main(String[] args) {
            launch(args);
        }

        @Override
        public void start(Stage stage) throws Exception {

            ObservableList<Employee> employees = FXCollections.observableArrayList(new Employee("John"), new Employee("Michael"));

            TableView<Employee> tableView = new TableView<>(employees);

            TableColumn<Employee, String> nameColumn = new TableColumn<>("Name");
            nameColumn.setCellFactory(param -> new TableCell<Employee, String>() {

                private final EmployeeNameCell employeeNameCell = new EmployeeNameCell();

                @Override
                protected void updateItem(String name, boolean empty) {
                    super.updateItem(name, empty);
                    if (name == null || empty) {
                        setGraphic(null);
                    } else {
                        employeeNameCell.setName(name);
                        setGraphic(employeeNameCell);
                    }
                }
            });

            nameColumn.setCellValueFactory(param -> param.getValue().nameProperty());

            tableView.getColumns().add(nameColumn);
            Scene scene = new Scene(tableView);
            stage.setScene(scene);
            stage.show();
        }
    }

    class Employee {

        private final StringProperty nameProperty = new SimpleStringProperty();

        public Employee(String name) {
            nameProperty.set(name);
        }

        public StringProperty nameProperty() {
            return nameProperty;
        }
    }

    class EmployeeNameCell extends HBox {

        private final TextField textField = new TextField();

        private final StringProperty nameProperty = new SimpleStringProperty();

        public EmployeeNameCell() {
            getChildren().add(textField);
            setMaxWidth(10e6);
            setHgrow(textField, Priority.ALWAYS);

            nameProperty.addListener((ChangeListener<String>) (observable, oldValue, newValue) -> {
                textField.setText(newValue);
            });
        }

        public void setName(String name) {
            nameProperty.set(name);
        }
    }