在 TreeTableView 中添加进度条(返回 Double 值给 Observable<Double>)

Add progressbar in TreeTableView (returning Double value to Observable<Double>)

我正在试验 JavaFX,我正尝试在树 table 中添加一个进度条。有没有办法link薪水值到进度条。

我的代码是 Oracle's TreeTableView 示例的修改版本:

public class TreeTableViewSample extends Application implements Runnable {

List<Employee> employees = Arrays.<Employee>asList(
        new Employee("Ethan Williams", 30.0),
        new Employee("Emma Jones", 10.0),
        new Employee("Michael Brown", 70.0),
        new Employee("Anna Black", 50.0),
        new Employee("Rodger York", 20.0),
        new Employee("Susan Collins", 70.0));

/*  private final ImageView depIcon = new ImageView (
 new Image(getClass().getResourceAsStream("department.png"))
 );
 */
final CheckBoxTreeItem<Employee> root
        = new CheckBoxTreeItem<>(new Employee("Sales Department", 0.0));
final CheckBoxTreeItem<Employee> root2
        = new CheckBoxTreeItem<>(new Employee("Departments", 0.0));

public static void main(String[] args) {
    Application.launch(TreeTableViewSample.class, args);
}

@Override
public void start(Stage stage) {
    root.setExpanded(true);
    employees.stream().forEach((employee) -> {
        CheckBoxTreeItem<Employee> cbt=new CheckBoxTreeItem<>(employee);
        cbt.selectedProperty().addListener((ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) -> {
                if (newValue) {
                    System.out.println("Slected employ name is "+cbt.getValue().getName()); 
                }
            });

        root.getChildren().add(cbt);
    });
    stage.setTitle("Tree Table View Sample");
    final Scene scene = new Scene(new Group(), 400, 400);
    scene.setFill(Color.LIGHTGRAY);
    Group sceneRoot = (Group) scene.getRoot();

    TreeTableColumn<Employee, String> empColumn
            = new TreeTableColumn<>("Employee");
    empColumn.setPrefWidth(150);
    empColumn.setCellValueFactory(
            (TreeTableColumn.CellDataFeatures<Employee, String> param)
            -> new ReadOnlyStringWrapper(param.getValue().getValue().getName())
    );

    TreeTableColumn<Employee, Double> salaryColumn
            = new TreeTableColumn<>("Salary");
    salaryColumn.setPrefWidth(190);
    salaryColumn.setCellValueFactory(new Callback<CellDataFeatures<Employee, Double>, ObservableValue<Double>>() {
  @Override
  public ObservableValue<Double> call(CellDataFeatures<Employee, Double> p)          {
// p.getValue() returns the Employee instance for a particular TableView row
      return p.getValue().getValue().getSalary();
 }
 });
    salaryColumn.setCellFactory(ProgressBarTreeTableCell.<Employee>forTreeTableColumn());
    root2.getChildren().add(root);

    TreeTableView<Employee> treeTableView = new TreeTableView<>(root2);
    treeTableView.getColumns().setAll(empColumn, salaryColumn);
    treeTableView.setRowFactory(f -> new CheckBoxTreeTableRowHack<>());
    sceneRoot.getChildren().add(treeTableView);
    stage.setScene(scene);
    stage.show();
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
    executorService.scheduleAtFixedRate(this, 3, 10, TimeUnit.SECONDS);

}

@Override
public void run() {
    root2.getValue().setSalary(calcSalary(root));
}

public double calcSalary(TreeItem<Employee> t) {
    Double salary = 0.0;
    if (!t.isLeaf()) {

        ObservableList<TreeItem<Employee>> al = t.getChildren();
        for (int i = 0; i < al.size(); i++) {
            TreeItem<Employee> get = al.get(i);
            salary += calcSalary(get);
        }
        t.getValue().setSalary(salary);
    }
    return salary += t.getValue().getSalary();
}

public class Employee {

    private SimpleStringProperty name;
    private SimpleDoubleProperty salary;

    public SimpleStringProperty nameProperty() {
        if (name == null) {
            name = new SimpleStringProperty(this, "name");
        }
        return name;
    }

    public SimpleDoubleProperty salaryProperty() {
        if (salary == null) {
            salary = new SimpleDoubleProperty(this, "salary");
        }
        return salary;
    }

    private Employee(String name, Double salary) {
        this.name = new SimpleStringProperty(name);
        this.salary = new SimpleDoubleProperty(salary);
    }

    public String getName() {
        return name.get();
    }

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

    public Double getSalary() {
        return salary.get();
    }

    public void setSalary(Double fName) {
        salary.set(fName);
    }
}
}

我在 return p.getValue().getValue().getSalary(); 收到错误,它说 Incompatible Types: Double cannot be converted to ObservableValue<Double>。据我所知,您必须在调用方法的 return 中添加 return ObservableValue。但是作为新手,我无法找到一种方法来表达价值。设置字符串很简单。

到目前为止,我已经尝试了 return new ReadOnlyDoubleWrapper(p.getValue().getValue().getSalary()); ,但对我来说也没有成功。

我正在使用 JavaFx 8。

我正在尝试创建工资条,它也可以用作任务及其子任务的进度条。 (只是玩 UI)。但不知道如何将它们与员工的真实价值联系起来,因为我猜正常 table 视图与树 table 视图不同。谢谢 ! :)

这与 JavaFX Properties in TableView 基本相同:salaryProperty 不是 ObservableValue<Double>,而是 ObservableValue<Number>

您需要将列设置为 TreeTableColumn<Employee, Number>。然后你可以 return 员工的 salaryProperty:

TreeTableColumn<Employee, Number> salaryColumn
        = new TreeTableColumn<>("Salary");
salaryColumn.setCellValueFactory(p -> p.getValue().getValue().salaryProperty());

另一种选择是使用 salaryProperty().asObject(),其中 return 是一个 ObjectProperty<Double> 双向绑定到 salaryProperty():

TreeTableColumn<Employee, Double> salaryColumn
        = new TreeTableColumn<>("Salary");
salaryColumn.setCellValueFactory(p -> 
    p.getValue().getValue().salaryProperty().asObject());