Is there a different way to style table rows in javafx - javafx

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.

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 */
}

Related

How to make cell color change on double click tornadofx

I need to make cells color in my tableview be changed by right mouse click. My code:
cellFormat { _ ->
graphicProperty().addListener { _ ->
setOnMouseClicked {
if (it.button == MouseButton.SECONDARY)
style {
backgroundColor += c("darkred")
}
}
}
}
second variant:
cellFormat { _ ->
style {
setOnMouseClicked { button ->
if (button.button == MouseButton.SECONDARY) {
backgroundColor += c("darkred")
}
}
} }
I understand that I need to make cell format listener, however I tried different ways and have no result. Can anyone give me a tip?
This sounds like you are marking selections for a later operation. Have you considered a multi-select list with a CSS style applied to selected items?
A checkbox could also be used to mark off records if you have need for a parallel selection mechanism.

Tornadofx Custom Table Cell

how to insert a button or any other kind of component in javafx tableview cell using tornadofx ?
I am in a situation where i have a column header "Action". I need to render several action buttons in the table view .
Use the cellFormat function and assign a container with the buttons to the graphic property of the cell:
column("Name", SomeObject::someproperty).cellFormat {
graphic = hbox(spacing = 5) {
button("Action 1").action { doSomething() }
button("Action 2").action { doSomethingElse() }
}
}

How to change the background color of the focussed table row

So I've a method that focuses the table row based on the row index. However, the default background color of the focused row is grey, which is not easily visible. I want to change this color to steelblue. Please suggest how to do it. Below is my code for the method.
public static void focusTableRow(TableView table, int rowIndex){
table.requestFocus();
table.getSelectionModel().select(rowIndex);
table.getFocusModel().focus(rowIndex);
table.scrollTo(rowIndex);
}
To style the rows, Add CSS in the external CSS file :
.table-row-cell:selected {
-fx-background: steelblue;
}
Update:
Save the content of this style in a rowStyles.css. then you have to add the URL for the rowStyles.css style sheet to the scene:
scene.getStylesheets().add(getClass().getResource("rowStyles.css").toExternalForm());

Highlighting multiple cells in a JavaFX TableRow

Jewelsea has provided a great example of highlighting table rows and an individual cell at GitHub. However, I am having great difficulty with something that is closely related.
Here is a screen shot:
Using his example, when I perform the updateItem code for an individual cell in the "Will Pay Up" column, I would also like to highlight the corresponding name (which would be found in the same TableRow) with the same color as the cell in the will pay up column. In the code I am actually developing I have ensured that name is always found in the first column.
Here are just a few lines from his code for updateItem (I hope this is okay):
// update the item and set a custom style if necessary
if (item != null) {
setText(item.toString());
this.getStyleClass().add(item ? "willPayCell" : "wontPayCell");
this.getTableRow().getStyleClass().add(item ? "willPayRow" : "wontPayRow");
}
We can highlight the current cell (this.getStyleClass()) or the entire row (this.getTableRow()) but I'm unable to find a way to access another cell within the current row.
Set the style class of the cells to something constant, and the style class of the rows depending on the item's value:
if (item != null) {
setText(item.toString());
if (! this.getStyleClass().contains("willPayCell")) {
this.getStyleClass().add("willPayCell");
}
this.getTableRow().getStyleClass().removeAll("willPayRow", "wontPayRow");
this.getTableRow().getStyleClass().add(item ? "willPayRow" : "wontPayRow");
}
and then you can just use the CSS to determine the colors of different cells:
.table-row-cell.willPayRow .table-cell {
-fx-background: /* color for cells other than willPayCell in willPayRow */;
}
.table-row-cell.wontPayRow .table-cell {
-fx-background: /* color for cells other than willPayCell in wontPayRow */;
}
.table-row-cell.willPayRow .willPayCell {
-fx-background: /* color for willPayCell in willPayRow */;
}
.table-row-cell.wontPayRow .willPayCell {
-fx-background: /* color for willPayCell in wontPayRow */;
}

JavaFX8 tree table view custom root row

In my tree-table-view I have a root item which contains child items(I have mistakenly called them root items) that in turn have child items. I need to customize those mistakenly called root items rows text appearance. Is there such a selector or how else would that be done?
Thanks.
This will set a pseudo-class on the row containing the root:
final PseudoClass firstRowClass = PseudoClass.getPseudoClass("first-row");
treeTableView.setRowFactory(treeTable -> {
TreeTableRow<...> row = new TreeTableRow<>();
row.treeItemProperty().addListener((ov, oldTreeItem, newTreeItem) ->
row.pseudoClassStateChanged(firstRowClass, newTreeItem == treeTable.getRoot()));
return row ;
});
Now you can select that row in css with
.tree-table-row-cell:first-row { ... }
Complete example here
It sounds like you want to style the immediate child nodes of the root node. In this case, just do
row.treeItemProperty().addListener((ov, oldTreeItem, newTreeItem) ->
row.pseudoClassStateChanged(firstRowClass,
newTreeItem != null && newTreeItem.getParent() == treeTable.getRoot()));
instead of the condition in the code above. Obviously, you can use other criteria as you need them (e.g. ! newTreeItem.isLeaf()).
Note that the default style sheet rules for tree-table-row are a little strange: -fx-background-color is set for the row, but -fx-text-fill is set for both the row and the cells inside it. So if you want to change the background color, you just need
-tree-table-row-cell:first-row {
-fx-background-color: antiquewhite ;
}
but if you want to change the text color, you need to change it on the cells:
-tree-table-row-cell:first-row .tree-table-cell {
-fx-text-fill: red ;
}

Resources