JavaFX:向 TreeTableView 添加 UI 控件 [英] JavaFX: Add UI control to TreeTableView
问题描述
假设我在 TreeTableView 中有 2 列,现在我想在第一列中添加一个字符串/标签,在另一列中添加一个 ProgressBar.我将如何完成这样的事情?
Let's say i have 2 columns in a TreeTableView and now i want to add a string/Label in the first column and a ProgressBar in the other one. How would i accomplish something like this?
非常感谢您的帮助!
推荐答案
正如 James_D 正确指出的那样,您可以将 ProgressBarTreeTableCell
用于带有 ProgressBar 的列.内部支持一些其他 UI 控件,例如 TextField
、CheckBox
等.
As correctly pointed out by James_D, you can use ProgressBarTreeTableCell
for a column with ProgressBars. There is internal supports for some other UI controls such as TextField
, CheckBox
etc.
对于其他 UI 控件,您可以创建自定义 TreeTableCell
如图:
For other UI controls you can create a Custom TreeTableCell
as shown:
private class ProgressCell extends TreeTableCell<Employee, String> {
final ProgressBar progress = new ProgressBar();
ProgressCell() {
}
@Override
protected void updateItem(String t, boolean empty) {
super.updateItem(t, empty);
if (!empty) {
setGraphic(progress);
}
}
}
然后为第二列分配一个CellFactory
secondCol.setCellFactory(
new Callback<TreeTableColumn<Employee, String>, TreeTableCell<Employee, String>>() {
@Override
public TreeTableCell<Employee, String> call(
TreeTableColumn<Employee, String> param) {
return new ProgressCell();
}
});
其中 Employee
是构建 TreeTableView 的 POJO 类
where Employee
is the POJO class on which the TreeTableView is built
这篇关于JavaFX:向 TreeTableView 添加 UI 控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!