14
我需要用多色行創建JavaFx TableView(color1爲低優先級,color2爲中等優先級等)。我創建CellFactoryJavaFX桌面顏色
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;}
但我仍然需要突出所選行。我怎樣才能做到這一點?
謝謝你))你能回答,我在哪裏可以找到這些css屬性列表(如-fx-control-inner-background,-fx-accent)或一些樣本? –
更新後的答案指向這些資源。 – jewelsea
我還有一個問題:我可以同樣突出顯示行中的單個單元嗎? –