Sub Button1_Click
tv.Items.Clear
'Create some random items
For i = 0 To 10
tv.Items.Add(Array( _
IIf(Rnd(0,2) > 0, "Bob", "Marc"), _
IIf(Rnd(0,2) > 0, "On", "Off"), _
IIf(Rnd(0,2) > 0, "Yes", "No") _
))
Next
TableViewSetCellColour(tv, CreateMap( _
"On": "-fx-background-color: rgba(255, 0, 0, 0.5); -fx-text-fill:white;", _
"Off": "-fx-background-color: rgba(0, 255, 0, 0.5);", _
"Yes": "-fx-background-color: pink;" _
))
End Sub
'theTable: The tableview to operate on
'TextVSStyleMap: A map of String and the corresponding CSS style to apply if matching.
'Original code from @Daestrum
Sub TableViewSetCellColour(theTable As TableView, TextVSStyleMap As Map)
Dim cols As List = theTable.as(JavaObject).RunMethodJO("getColumns", Null)
Dim i As Int = 1
Dim joMe As JavaObject = Me
For Each col As JavaObject In cols
col.RunMethodJO("getStyleClass", Null).RunMethod("add", Array("col" & i))
joMe.RunMethod("setColumnColour", Array(col, TextVSStyleMap))
i = i + 1
Next
End Sub
#if java
import javafx.scene.control.*;
import javafx.util.Callback;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.paint.Color;
import java.util.Map;
public static void setColumnColour(TableColumn tc, Map<String, String> textStyleMap) {
tc.setCellFactory(new Callback<TableColumn, TableCell>() {
@Override
public TableCell call(TableColumn param) {
return new TableCell<Object, Object>() {
@Override
public void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (!isEmpty()) {
if (textStyleMap.containsKey(String.valueOf(item))) {
String style = textStyleMap.get(String.valueOf(item));
setStyle(style);
} else {
setStyle("");
}
setText(String.valueOf(item));
}
}
};
}
});
}
#End If