Dil*_*aur 6 javafx javafx-2 javafx-8
我有以下代码来更新列单元格及其相应行的颜色:
calltypel.setCellFactory(column -> {
return new TableCell<CallLogs, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : getItem().toString());
setGraphic(null);
TableRow currentRow = getTableRow();
//This doesn't work
if(item.equals("a")){
item.setTextFill(Color.RED);
currentRow.setTextFill(Color.PINK);
}
else{
item.setTextFill(Color.GREEN);
currentRow.setTextFill(Color.BLUE);
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
'if'条件的代码段不起作用.我无法确定对象的正确引用,以及执行此操作的最佳方法是什么.
谢谢!
Dil*_*aur 12
private void customiseFactory(TableColumn<CallLogs, String> calltypel) {
calltypel.setCellFactory(column -> {
return new TableCell<CallLogs, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? "" : getItem().toString());
setGraphic(null);
TableRow<CallLogs> currentRow = getTableRow();
if (!isEmpty()) {
if(item.equals("a"))
currentRow.setStyle("-fx-background-color:lightcoral");
else
currentRow.setStyle("-fx-background-color:lightgreen");
}
}
};
});
}
Run Code Online (Sandbox Code Playgroud)
这有效!
Joe*_*ore 10
正确的做法是使用setRowFactory表格:
table.setRowFactory(tv -> new TableRow<CustomItem>() {
@Override
protected void updateItem(CustomItem item, boolean empty) {
super.updateItem(item, empty);
if (item == null || item.getValue() == null)
setStyle("");
else if (item.getValue() > 0)
setStyle("-fx-background-color: #baffba;");
else if (item.getValue() < 0)
setStyle("-fx-background-color: #ffd7d1;");
else
setStyle("");
}
});
Run Code Online (Sandbox Code Playgroud)
我最近对这个问题做了一些研究。使用以下代码,您可以根据列值更改TableView的行颜色(我将尽我所能解释)。
我们要做的第一件事是定义TableView和此TableView的Columns:
private TableView<Person> personTable;
private TableColumn<Person, String> nameColumn;
private TableColumn<Person, String> lastNameColumn;
Run Code Online (Sandbox Code Playgroud)
下一步是定义以下各列之一的“单元工厂”:
nameColumn.setCellFactory(column -> {
return new TableCell<Person, String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty); //This is mandatory
if (item == null || empty) { //If the cell is empty
setText(null);
setStyle("");
} else { //If the cell is not empty
setText(item); //Put the String data in the cell
//We get here all the info of the Person of this row
Person auxPerson = getTableView().getItems().get(getIndex());
// Style all persons wich name is "Edgard"
if (auxPerson.getName().equals("Edgard")) {
setTextFill(Color.RED); //The text in red
setStyle("-fx-background-color: yellow"); //The background of the cell in yellow
} else {
//Here I see if the row of this cell is selected or not
if(getTableView().getSelectionModel().getSelectedItems().contains(auxPerson))
setTextFill(Color.WHITE);
else
setTextFill(Color.BLACK);
}
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
代码的逻辑:我们覆盖的updateItem()方法,当基础项目更改时会自动调用它。
我们收到必须呈现的数据项(在这种情况下为String)。如果该项为空或为空(例如,一个空单元格),则我们不应用任何样式。否则,我们将格式化项目,设置单元格的文本以及颜色和背景,具体取决于人员的姓名。
如果要在表格的其他列中应用单元格的这种颜色,我们必须使用“行工厂”而不是“单元工厂”,但是代码的逻辑类似:
personTable.setRowFactory(row -> new TableRow<Person>(){
@Override
public void updateItem(Person item, boolean empty){
super.updateItem(item, empty);
if (item == null || empty) {
setStyle("");
} else {
//Now 'item' has all the info of the Person in this row
if (item.getName().equals("Edgar")) {
//We apply now the changes in all the cells of the row
for(int i=0; i<getChildren().size();i++){
((Labeled) getChildren().get(i)).setTextFill(Color.RED);
((Labeled) getChildren().get(i)).setStyle("-fx-background-color: yellow");
}
} else {
if(getTableView().getSelectionModel().getSelectedItems().contains(item)){
for(int i=0; i<getChildren().size();i++){
((Labeled) getChildren().get(i)).setTextFill(Color.WHITE);;
}
}
else{
for(int i=0; i<getChildren().size();i++){
((Labeled) getChildren().get(i)).setTextFill(Color.BLACK);;
}
}
}
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是我发现在行的所有单元格中应用样式更改的最佳方法。如果在单元工厂中使用方法“ getTableRow()”,则无法修改其单元子级。
注意1:如果要更改文本的样式,则必须在单元格中进行操作。如果您尝试直接在行上执行此更改,则无效。
注意2:如果您使用的是单独的CSS文件,请不要编写以下内容:
.table-cell {
-fx-text-fill: Black;
}
Run Code Online (Sandbox Code Playgroud)
因为如果执行此操作,则所有Java代码均无效。
| 归档时间: |
|
| 查看次数: |
29401 次 |
| 最近记录: |