[英]JavaFX_TableView_How to fill in a color to a certain cell if a hashmap is used
I have created a Java TableView using a hashmap for the data. 我使用数据的哈希表创建了Java TableView。 Within the hashmap, the key is a certain word, and the value is how often this word is included.
在哈希图中,关键字是某个单词,值是包含该单词的频率。 I also implemented an ObservableList for the dataModel.
我还为dataModel实现了一个ObservableList。
Let´s say, we have a tableview in which the first column represents capital cities (key of hashmap) and the second column represents how often a certain city has been visited (value of hashmap), ie 假设我们有一个表格视图,其中第一列代表首都城市(哈希图的键),第二列代表某座城市的访问频率(哈希图的值),即
Cities No. of times visited
Amsterdam 2
Berlin 4
Rome 6
I now want to fill certain cells in the 2nd column with a color. 我现在想用颜色填充第二列中的某些单元格。 For example, all cells with a value of greater than 2 should be yellow, values >4 should be orange and values >6 should be red.
例如,所有大于2的单元格应为黄色,大于4的值应为橙色,而大于6的值应为红色。 But I don´t know how to do that.
但是我不知道该怎么做。 Here are some code snippets which show what I alreday did:
以下是一些代码片段,这些片段显示了我日常的工作:
//create HashMap
HashMap<String, Integer[]> listMap;
//ObservableList
public static ObservableList<Map.Entry<String, Integer[]>> items;
//create table
TableView<Map.Entry<String, Integer[]>> table = new TableView<>(items);
//column words (key of hashmap)
words.setCellValueFactory(new PropertyValueFactory<>("words"));
//return property from hashmap for cell
words.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map.Entry<String, Integer[]>, String>, ObservableValue<String>>()
{
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Map.Entry<String, Integer[]>, String> p)
{
return new SimpleStringProperty(p.getValue().getKey());
}
});
firstColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map.Entry<String, Integer[]>, String>, ObservableValue<String>>()
{
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<Map.Entry<String, Integer[]>, String> p)
{
return new SimpleStringProperty(String.valueOf(p.getValue().getValue());
}
});
Does anyone have a solution to this issue? 有谁能解决这个问题? Thanks :)
谢谢 :)
Use the cell factory to define the rendering behavior of the cell. 使用单元格工厂定义单元格的渲染行为。 The cell value factory only defines the populating behavior.
单元格值工厂仅定义填充行为。
Example: 例:
firstColumn.setCellFactory(column -> {
return new TableCell<YourObject, Integer>() {
@Override
protected void updateItem(Integer item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
} else {
if(item > 2)
setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));
else if(item > 4)
setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
else
setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
}
}
};
});
If you want to display a text associated with the value, let's say "OK" when value < 2, "Warning" when value > 2 and "Danger" when value > 4, you can do it with setText("Some text")
just next to the setBackground()
. 如果要显示与该值关联的文本,则当值<2时说“确定”,当值> 2时说“警告”,而值> 4时说“危险”,可以使用
setText("Some text")
就在setBackground()
旁边。 The cell will then display the text depending on the value of the cell. 然后,单元格将根据单元格的值显示文本。
if (item == null || empty) {
setText("");
setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
} else {
if(item > 2) {
setText("Warning");
setBackground(new Background(new BackgroundFill(Color.YELLOW, CornerRadii.EMPTY, Insets.EMPTY)));
} else if(item > 4) {
setText("Danger");
setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
} else {
setText("OK");
setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.