-1

I currently am using a custom cell factory in Javafx to style cells/rows of my table view with css. This is working successfully and exactly how I need it to. I was wondering if there was another way to style the rows of a table view.

I want to style the entire row with css dynamically instead of cell by cell. Some of the rows will be different colors, etc. Font fill, background color, font size, etc.. nothing fancy.

Hypnic Jerk
  • 1,192
  • 3
  • 14
  • 32

1 Answers1

3

You can use a rowFactory on the table which generates rows and manipulates either the style class of the row or a pseudoclass attached to the row. Then use an external style sheet to apply the styles.

e.g.

PseudoClass foo = PseudoClass.getPseudoClass("foo");
table.setRowFactory(tv -> {
    TableRow<MyDataType> row = new TableRow<>();
    row.itemProperty().addListener((obs, oldItem, newItem) -> {
        if (/* some condition on newItem */) {
             row.pseudoClassStateChanged(foo, true);
        } else {
             row.pseudoClassStateChanged(foo, false);
        }
    });
    return row ;
});

and then

.table-row-cell {
    /* your regular style settings here */
}

.table-row-cell:foo {
    /* your specific style for when foo is set here */
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Not exactly what I was looking for, but still very useful to me. I was looking for something more along the lines of styling an entire row dynamically, instead of one cell at a time, like cell factory does. Will edit my question to have more clarity. – Hypnic Jerk Sep 07 '16 at 16:35
  • That is what I was looking for. Thank you very much. – Hypnic Jerk Sep 07 '16 at 16:44
  • @MichaelPickett See http://stackoverflow.com/questions/20350099/programmatically-change-the-tableview-row-appearance/20409924#20409924 for a related question. – James_D Sep 07 '16 at 16:44
  • Thank you for the link. It definitely helps a lot. – Hypnic Jerk Sep 07 '16 at 16:48