Highlighting multiple cells in a JavaFX TableRow - javafx

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

Related

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());

Is there a different way to style table rows in 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 */
}

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 ;
}

Change the background color of a column in a grid

I have following form and I want to change the background color of a column, based on the values of other columns;
In the orange columns, instead of displaying orange background, I want the cell color to be the RGB combo of Red, Green & Blue fields under COLOR ATTRIBUTES section.
Let's say that the control the background of which you need to change is named FirstFieldControl. Set its AutoDeclaration property to Yes and BackgroundColor to Window background.
Now you need to override the displayOption method on your datasource, e.g.:
public void displayOption(Common _record, FormRowDisplayOption _options)
{
YourTable yourTable = _record;
int color;
;
switch (yourTable.Name)
{
case 'Red' :
color = WINAPI::rgbCon2int([255, 0, 0]);
break;
case 'Green' :
color = WINAPI::rgbCon2int([0, 255, 0]);
break;
case 'Blue' :
color = WINAPI::rgbCon2int([0, 0, 255]);
break;
}
if (color)
{
_options.backColor(color);
_options.affectedElementsByControl(FirstFieldControl.id());
}
else
{
super(_record, _options);
}
}
This is just an example to give you an idea - don't copy-paste :)
It's easier to store the color value in the table, then the code will be much nicer.
P.S. If you're changing the colors run-time you might need to use the following piece of code to refresh the record:
yourTable_ds.clearDisplayOption(yourTable);
yourTable_ds.refresh();
I want to show variable color according to warehouse in on-hand form. If I override displayOption on inventdim datasource, it does get called. It does get called if I override InventSum datasource. But I cannot get the actual inventdim record. In this form, InventSum is master table, and InventDim is the joined child table.

Making a single row of an aspxGridView flash

Is there any way of making a single row within an AspxGridView flash different colours based on a value within a cell. E.g. continually changing the background colour of the row between red and green if a value in a cell is 5 so that it stands out on the page?
I have found one article that says it cant be done, but this was in 2008..
http://www.devexpress.com/Support/Center/p/Q135996.aspx
With a css class added to row as advised in the previous answer you can apply following script and style:
$(function () {
setInterval(flashRow, 500);
});
function flashRow() {
$("tr.blink").toggleClass("red");
}
Css style:
tr.blink
{
background-color: Green;
}
tr.red
{
background-color: Red;
}
Where blink - css style that you add to the row in the RowDataBound method.
See demo here
Take a look at the E3324 Code Central example.
You can use the described approach as a starting point.
<dx:ASPxTimer ID="ASPxTimer2" runat="server" Interval="250"
ClientSideEvents-Tick="function(s,e)
{
var table = document.getElementById(gridUsers.name);
for (i = 0; i <= table.rows.length; i++)
{
var tableRow = document.getElementById(gridUsers.name + '_DXDataRow' + i);
if (tableRow.getAttribute('flicker') != '1')
return;
if (tableRow.style.backgroundColor == '' || tableRow.style.backgroundColor == 'white')
tableRow.style.backgroundColor = 'red';
else
tableRow.style.backgroundColor = 'white';
}
}">
</dx:ASPxTimer>
you can do that on the event rowdatabount
check the current row is it with the value you want to highlight
then change the color of the current row by adding css attributes to it or assigning a cssclass

Resources