- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要你的帮助!
我有一个表格,里面有行(姓名等)现在,当位于该行上的对象具有特定值时,我想为特定的 tableCells 背景着色。但我只得到它来读取这个单元格的值。但我需要阅读对象(在我的代码中称为 TableListObject
)以了解我需要用哪种颜色为单元格着色。但是此“颜色值”在该行中不可见(没有列)。
这是我的代码:
for(TableColumn tc:tView.getColumns()) {
if(tc.getId().equals("text")) {
tc.setCellValueFactory(newPropertyValueFactory<TableListObject,String>("text"));
// here i need to check the Objects value and coloring that cell
}
}
这是一个 HTML Fiddle 来可视化我的问题: https://jsfiddle.net/02ho4p6e/
最佳答案
为您想要的列调用单元工厂并覆盖 updateItem
方法。您需要检查它是否为空,如果不是,您可以进行对象检查,然后您可以设置单元格背景的颜色或您想要的任何其他样式。希望这会有所帮助。
tc.setCellFactory(column -> {
return new TableCell<TableListObject, String>() {
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setStyle("");
} else {
if (item.equals("Something")) {
setStyle("-fx-background-color: blue");
} else {
setStyle("");
}
}
}
};
});
编辑 1:
如果要使用同一行中另一个单元格的值。您将不得不使用行的索引并获取检查所需的项目。
tc.setCellFactory(column - > {
return new TableCell < TableListObject, String > () {
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setText(null);
setStyle("");
} else {
int rowIndex = getTableRow().getIndex();
String valueInSecondaryCell = getTableView().getItems().get(rowIndex).getMethod();
if (valueInSecondaryCell.equals("Something Else")) {
setStyle("-fx-background-color: yellow"); //Set the style in the first cell based on the value of the second cell
} else {
setStyle("");
}
}
}
};
});
编辑 2:
根据建议改进了答案。这使用引用的对象。
else {
TableListObject listObject = (TableListObject) getTableRow().getItem();
if (listObject.getMethod().equals("Something Else")) {
setStyle("-fx-background-color: yellow"); //Set the style in the first cell based on the value of the second cell
} else {
setStyle("");
}
}
关于JavaFX 着色 TableCell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35056108/
我有一个包含单元格的 TableView。我的表格的工作方式类似于坐标系,而不是像通常的表格。 Click here for more about this. 现在,我需要将一个单元格拖放到另一个单元
我目前正在开发一个应用程序,其中使用 TableView,其中包含一个 TableCell 中的 RadioButton。为此,我创建了一个自己的 RadioButtonCell 类。我还有一个“添加
我在 tableviewcell(原型(prototype)单元格)内添加了一个标签,并使用 Storyboard对标签应用了以下约束: 50 领先利润率 50 尾随 margin 20 上边距 20
我需要放入一个表格单元格、一个标签和进度条。 对于进度条,我使用的是: @FXML private TableView tableView; @FXML private TableColumn col
我对表格单元格中的分段控件有疑问。分段控制无 Action segmentedFilter.addTarget(self, action: #selector(ProductListFilterCel
我需要你的帮助! 我有一个表格,里面有行(姓名等)现在,当位于该行上的对象具有特定值时,我想为特定的 tableCells 背景着色。但我只得到它来读取这个单元格的值。但我需要阅读对象(在我的代码中称
JavaFX:如何使用自定义样式表将文本颜色应用于 TableCell? 当我直接在我的 CellFactory 中使用 setTextFill() 时它工作正常,但我想使用外部 CSS 文件应用自定
我想从 TableCell 中的模型获取一个属性,这样我就可以根据该属性来操纵单元格,让我们看一下以下示例: 我有一个像这样的模型: public class Model { private
我的应用程序有一个 TableView,其中填充了对图像文件的引用列表。 数据从数据库加载,仅提供有关如何定位图像文件本身的信息(因此它指示图像的子文件夹和文件名)。 在我的 TableView 中,
如何确定 javafx 表单元格引用的数据类型(字符串、整数、 double 等)。例如。我有一个自定义单元工厂,它被分配给 Javafx Controller 中的一个字段,如下所示: Custom
这不是一个特定于语言的问题,但我需要它来玩 java 游戏。我有一个尺寸为 3x3 的表(二维数组)。所以单元格的一维索引是: 1 2 3 4 5 6 7 8 9 当有这个索引时,我想从中获取二维索引
例如,当您按下 Enter 键时,TextFieldTableCell 会响应并调用 commitEdit()。 如果我使用自定义节点来显示,那么我如何监听确认编辑的方法?例如,我有一个 DateTi
我正在尝试将 tableCell 添加到 TableView 中的适当部分。我有一个按如下方式索引的客户端列表: var clients = Client.loadAllClients()
Xcode: swift 完全错误: 无法找到接受 caseImage 类型的参数列表的类型 TableCell 的初始值设定项:(UIImge, caseName: String, caseDate
我最近遇到了一个有趣的tutorial在 Ray Wenderlich 的网站上,了解如何通过使用长按手势并拖动 TableCell 来重新定位 TableCell,以更改顺序。一切正常,但我相信它会
我创建了一个自定义 tableCell 当我通过 backgroundView 将 xib 连接到文件所有者时,我成功地让项目出现在我的表中。问题是我无法选择一行并获得返回。 如果我取消选择 back
我正在尝试使用 segue 将注释传递给 mapkit 但是当我测试它时总是以 fatal error 结束:在展开可选值时意外发现 nil annotation is not a nil obj b
我的代码如下。我想在 tableCell() 中的单元格之间添加空间相当于的东西 我不是在寻找 cellpadding 或 cellspacing,因为它们都在左单元格边框和
我必须根据显示的数据更改 TableCell 的样式类,例如:如果两个连续行中的单元格值相同,则必须突出显示该单元格(即:背景红色)。这必须既能将数据添加到表中又能按任何列排序。 为此,在排序时,我向
我正在尝试在我的 TableView 中创建自定义 TableCell。我希望它显示一个 ComboBox,我可以在其中选择一个 String 值,然后显示 String 值,就好像它是用户输入一样。
我是一名优秀的程序员,十分优秀!