1

我正在尝试使用 JavaFX,并且正在尝试在树表中添加进度条。有没有办法将薪水的值链接到进度条。

我的代码是Oracle 的 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>。据我所知,您必须在调用方法的返回中返回 ObservableValue。但作为新手,我无法找到一种方法来创造价值。设置字符串很容易。

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

我正在使用 JavaFx 8。

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

4

1 回答 1

2

这与TableView 中的 JavaFX 属性基本相同:salaryProperty不是一个,ObservableValue<Double>而是一个ObservableValue<Number>.

您需要将列设为 a TreeTableColumn<Employee, Number>。然后你可以只salaryProperty为员工返回:

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

另一个选项是使用salaryProperty().asObject()which 返回一个ObjectProperty<Double>双向绑定到salaryProperty()

TreeTableColumn<Employee, Double> salaryColumn
        = new TreeTableColumn<>("Salary");
salaryColumn.setCellValueFactory(p -> 
    p.getValue().getValue().salaryProperty().asObject());
于 2015-03-31T14:56:27.740 回答