-1

My application contains:

  • An arraylist of integers. this list update every few second (numbers are added and other are deleted)

  • Tableview<Data>

  • the Data contains several fields and semNumId field (which is integer)

  • I want to be able to paint the rows, which the arraylist contains the semNumId numbers with green colour.

  • the arraylist may be change (and the tableview data will contain the same data). so the green colour may be set on and off without changing the table data

I have look here:

JavaFx How to set row background color of specifics rows in TableView

but seems it doesn't help much.

How can I implement this ?

pay attention that:

tableView.setRowFactory(tv -> {
    TableRow<Data> row = new TableRow<>();

create new tableRow which I cant check if it's semNumId in the list (because when creating with new operation the default is 0);

Please add example code

  • Create and post a [MCVE], showing what the issue is. In particular, it's not clear why the post you linked doesn't help. – James_D Jun 29 '17 at 11:27
  • In other words, "Please add example code" is what you should be doing, not asking others to do. – James_D Jun 29 '17 at 13:18

1 Answers1

1

Not sure if I understood you correctly...

private final ObservableList<Integer> numbers = FXCollections.observableArrayList<>();
private final BooleanProperty numbersChanged = new SimpleBooleanProperty(false);

numbers.addListener(new ListChangeListener<Number> {
        @Override public void onChanged(Change<? extends Number> c) {
            numbersChanged.set(true);
        }
    });

tableView.setRowFactory(tv -> {
    TableRow<Data> row = new TableRow<>();
    BooleanBinding contains = Bindings.createBooleanBinding(() -> {
        if (numberChanged.get()) {
            if (!numbers.isEmpty() && row.getItem() != null && numbers.contains(row.getItem().getSemNumId())) {
                return true;
            }
            numberChanged.set(false);
        }

        return false;
    }, row.itemProperty(), numbersChanged);
    row.styleProperty().bind(Bindings.when(contains)
        .then("-fx-background-color: green;")
        .otherwise(""));
    return row;
    });

This implementation assumes that your Data object will not change its value (semNumId) at runtime (i.e. immutable).

Jai
  • 8,165
  • 2
  • 21
  • 52
  • it doesnt work. the numbers object has the needed numbers, but the the condition "if (!numbers.isEmpty() && row.getItem() != null && numbers.contains(row.getItem().getSemNumId())) " is not true becuse row.getItem is always NULL –  Jun 29 '17 at 10:27
  • It's possible the `BooleanBinding contains` is getting garbage collected. You may need to make it a property of the `row` (i.e. use an anonymous subclass of `TableRow`, make `contains` a field, and bind the style in the constructor). – James_D Jun 29 '17 at 11:47
  • mm didn't understand how to do it ? –  Jun 29 '17 at 12:09
  • @Boom It doesn't seem to be necessary anyway. This solution works just fine as it is. – James_D Jun 29 '17 at 13:17