15

i need create JavaFx TableView with multicolor rows (color1 for low priority, color2 for medium priority etc.). I have created 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;
} }

and css

.priorityLow{ -fx-background-color: palegreen; }
.priorityMedium{ -fx-background-color: skyblue;}
.priorityHigh{ -fx-background-color: palevioletred;}

But i still need highlight selected rows. How can i do that?

Aubin
  • 14,617
  • 9
  • 61
  • 84
Olga Semernitskaya
  • 203
  • 1
  • 4
  • 8

1 Answers1

19

Instead of setting the background color for the entire cell in your css, just set the -fx-control-inner-background. Then you will have the default accent, hover and focus rings still available. Also remove the if statement around your setPriorityStyle call of course.

If you also want to override things like the default accent (selected) color or the hover color, you can also do this as in the css below - not sure if the highlight overrides are really recommended though, guess it would depend on your app and desired user experience.

.priorityLow { 
  -fx-control-inner-background: palegreen;
  -fx-accent: derive(-fx-control-inner-background, -40%);
  -fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}

.priorityMedium { 
  -fx-control-inner-background: skyblue;
  -fx-accent: derive(-fx-control-inner-background, -40%);
  -fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}

.priorityHigh { 
  -fx-control-inner-background: palevioletred;
  -fx-accent: derive(-fx-control-inner-background, -40%);
  -fx-cell-hover-color: derive(-fx-control-inner-background, -20%);
}

rowhighlight


Detailed styling information for JavaFX can be found in the default caspian.css stylesheet for JavaFX 2.2 and the JavaFX 2 CSS reference guide. To find caspian.css for your version of JavaFX you can unjar jfxrt.jar (sometimes found in the jre/lib directory).

Update

The default stylesheet for JavaFX is now named modena.css rather than caspian.css.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • 1
    Thanks you)) Can you answer, where can i find list of these css-properties (like -fx-control-inner-background, -fx-accent) or some samples? – Olga Semernitskaya Dec 04 '12 at 09:12
  • I have another question: can i similarly highlight single cell in row? – Olga Semernitskaya Jan 06 '13 at 14:15
  • Yes, set a a custom [cell factory](http://docs.oracle.com/javafx/2/api/javafx/scene/control/TableColumn.html#cellFactoryProperty) for the column containing the cell and in the cell factory provide cells which can manipulate their own style as required. If you need more detail, post a new question and I'm sure you'll get further info on how to accomplish this. – jewelsea Jan 06 '13 at 22:18
  • Yes Aubin, appears the solution no longer works with Java 8...such a shame. – jewelsea May 27 '14 at 06:13