我需要创建具有多色行的 JavaFx TableView(color1 表示低优先级,color2 表示中等优先级等)。我创建了 CellFactory
public class TaskCellFactory implements Callback<TableColumn, TableCell> {
@Override
public TableCell call(TableColumn p) {
TableCell cell = new TableCell<Task, Object>() {
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
setText(empty ? null : getString());
setGraphic(null);
TableRow currentRow = getTableRow();
Task currentTask = currentRow == null ? null : (Task)currentRow.getItem();
if(currentTask != null){
Priority priority = currentTask.getPriority();
clearPriorityStyle();
if(!isHover() && !isSelected() && !isFocused()){
setPriorityStyle(priority);
}
}
}
@Override
public void updateSelected(boolean upd){
super.updateSelected(upd);
System.out.println("is update");
}
private void clearPriorityStyle(){
ObservableList<String> styleClasses = getStyleClass();
styleClasses.remove("priorityLow");
styleClasses.remove("priorityMedium");
styleClasses.remove("priorityHigh");
}
private void setPriorityStyle(Priority priority){
switch(priority){
case LOW:
getStyleClass().add("priorityLow");
break;
case MEDIUM:
getStyleClass().add("priorityMedium");
break;
case HIGH:
getStyleClass().add("priorityHigh");
break;
}
System.out.println(getStyleClass());
}
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
};
return cell;
} }
和CSS
.priorityLow{ -fx-background-color: palegreen; }
.priorityMedium{ -fx-background-color: skyblue;}
.priorityHigh{ -fx-background-color: palevioletred;}
但我仍然需要突出显示选定的行。我怎样才能做到这一点?