gpt4 book ai didi

java - 如何为 TableView 使用自定义 cellFactories

转载 作者:搜寻专家 更新时间:2023-10-31 08:20:32 24 4
gpt4 key购买 nike

如何轻松地将自定义(可重用)cellFactories 应用到 javafx.scene.control.TableCell;javafx.scene.control.TableView;

最佳答案

要应用自定义 CellFactories 来格式化单元格中显示的文本、颜色等,以下示例可能会有很好的帮助。

初始情况

假设您有一个 Bean/POJO Person:

public class Person {
private double levelOfGrowth = 0;

public Person() {};

public Person(double levelOfGrowth) {
this.levelOfGrowth = levelOfGrowth;
};

public double getLevelOfGrowth() {
return levelOfGrowth;
}
}

其中 levelOfGrowth 是一个人的成长已经完成多少的百分比值。

它可能设置在 0.00 到 1.00 之间。

我们还假设您已经在绑定(bind)到 Controller MainWindowController 的 FXML 中创建了 View 。您还设置列以显示 levelOfGrowth 到 id levelOfGrowthColumn

public class MainWindowController implements Initializable {
@FXML
public TableColumn levelOfGrowthColumn;

/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<Person> persons = FXCollections.observableArrayList();

persons.add(new Person(0));
persons.add(new Person(0.5));
persons.add(new Person(1));

levelOfGrowthColumn.setCellValueFactory(new PropertyValueFactory<Person, Double>("levelOfGrowth"));

}
}

意图

上例中的问题是 View 中显示的值类似于 0.5 而不是 50%。我们还可能希望将 100% 等值的颜色更改为绿色。

解决方案

我们需要的是一个(可重用的)类,它描述了如何在 View 中格式化/打印 double 值,并将该类与 levelOfGrowthColumn 连接起来。

“格式化程序”类

public class PercantageFormatCell extends TableCell<Object, Double> {

public PercantageFormatCell() {
}

@Override
protected void updateItem(Double item, boolean empty) {
super.updateItem(item, empty);

// If the row is not empty but the Double-value is null,
// we will always display 0%
if (!empty && null == item) {
item = new Double(0.0d);
}

// Here we set the displayed text to anything we want without changing the
// real value behind it. We could also have used switch case or anything you
// like.
setText(item == null ? "" : NumberFormat.getPercentInstance().format(item));

// If the cell is selected, the text will always be white
// (so that it can be read against the blue background),
// if the value is 1 it will be green.
if (item != null) {
double value = item.doubleValue();
if (isFocused() || isSelected() || isPressed()) {
setTextFill(Color.WHITE);
} else if (value < 1) {
setTextFill(Color.BLACK);
} else {
setTextFill(Color.GREEN);
}
}
}
}

在你的 Controller 中使用它

public class MainWindowController implements Initializable {
@FXML
public TableColumn levelOfGrowthColumn;

/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
ObservableList<Person> persons = FXCollections.observableArrayList();

persons.add(new Person(0));
persons.add(new Person(0.5));
persons.add(new Person(1));

// New code START

// In case we have multiple columns with percent-values it
// might come in handy to store our formatter
Callback<TableColumn, TableCell> percantageCellFactory =
new Callback<TableColumn, TableCell>() {
public TableCell call(TableColumn p) {
return new PercantageFormatCell();
}
};

// Now all we have to do is to apply it
levelOfGrowthColumn.setCellFactory(percantageCellFactory);

// New code END

levelOfGrowthColumn.setCellValueFactory(new PropertyValueFactory<Person, Double>("levelOfGrowth"));

}
}

关于java - 如何为 TableView 使用自定义 cellFactories,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12027285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com