How do I add RowFactory to FilteredTableView?

53 views
Skip to first unread message

Marco

unread,
Apr 28, 2020, 7:53:56 AM4/28/20
to ControlsFX
I switched from regular TableView to FilteredTableView but my RowFactory isn't working anymore. This is my old RowFactory:

tableViewMain.setRowFactory(row -> new TableRow<Order>() {
   @Override
   protected void updateItem(Order item, boolean empty) {
      super.updateItem(item, empty);
      getStyleClass().removeAll(CSS_DUPLICATE, CSS_CUSTOMER, CSS_WINNER, CSS_PAID);
      if(item != null) {
         if(item.getiSumOfWawiOrders() > 0) {
            getStyleClass().add(CSS_CUSTOMER);
         }
         if(item.isIsCaptured()) {
            getStyleClass().add(CSS_PAID);
         }
         else if(item.isIsWinner()) {
            getStyleClass().add(CSS_WINNER);
         }
         else if(item.isIpConflict()){
            getStyleClass().add(CSS_DUPLICATE);
         }
      }
   }
});

I also tried with TableRow2 but I have no clue how to do it. Thx for help! Regards

Marco

unread,
May 4, 2020, 6:27:02 AM5/4/20
to ControlsFX
any help?

Samir Hadzic

unread,
May 4, 2020, 7:17:12 AM5/4/20
to ControlsFX
Hi,

Can you post an http://sscce.org/ to demonstrate the issue?

We cannot do anything with your code as it is filled with objects unknown to us (internal to your code)

Regards,

Marco

unread,
May 4, 2020, 7:47:50 AM5/4/20
to ControlsFX
Hi Samir,

thx for reply. There is nothing more to know. The unknown class Order is not necessary. To clarify: The code you see above works perfect as it is while using tableViewMain as TableView. After switching from TableView to FilteredTableView the untouched Rowfactory isn't working anymore. My Question is: Why isn't it working anymore, does FilteredTableView needs a RowFactory of Type TableRow2? A little bit shorten:

FilteredTableView<Order> tableViewMain = new FilteredTableView<>();
tableViewMain.setRowFactory(row -> new TableRow<Order>() {
   @Override
  protected void updateItem(Order item, boolean empty) {
     super.updateItem(item, empty);
     if(item != null) {
           getStyleClass().add("-fx-control-inner-background: rgba(255, 19, 23, 0.56);");
     }
  }
});

and I also tried this:

FilteredTableView<Order> tableViewMain = new FilteredTableView<>();
tableViewMain.setRowFactory(row -> new TableRow2<Order>(tableViewMain) {
   @Override
  protected void updateItem(Order item, boolean empty) {
     super.updateItem(item, empty);
     if(item != null) {
           getStyleClass().add("-fx-control-inner-background: rgba(255, 19, 23, 0.56);");
     }
  }
});

If it's easier for you: How do I add a RowFactory to a FilteredTableView? 

Regards

Samir Hadzic

unread,
May 4, 2020, 8:19:06 AM5/4/20
to ControlsFX
Hi,


You will see this comment:
//Bind the row factory, useful to handle the row freezing and row height.
// It prevents from setting a different row factory, unless developer
// chooses to unbind it and provide on his own risk a different row
// factory that may or may not work with TableView2 and row freezing.
tableView.rowFactoryProperty().bind(Bindings.createObjectBinding(
() -> param -> new TableRow2(tableView), tableView.skinProperty()));

So I guess it is not possible to override the rowFactory out of the box. I'm not familiar with TableView2 which seems to be a fork or my SpreadsheetView. Overriding the rowFactory may mess with some features.

Marco

unread,
May 4, 2020, 8:28:08 AM5/4/20
to ControlsFX
Thx a lot. So you know no other way to color a row? 

Samir Hadzic

unread,
May 4, 2020, 8:52:57 AM5/4/20
to ControlsFX
Hi,

Well, changing the color with some conditions like you did seems a bit difficult.

However, you could still override the rowFactory like that (my code is rom HelloFilteredTableView in the samples):

 Callback<TableView<Person>, TableRow<Person>> rowFactory = row -> new TableRow2(this) {
               
@Override
               
protected void updateItem(Object item, boolean empty) {

                   
super.updateItem(item, empty);
                   
if (item != null) {

                       
System.out.println(this);
                        setStyle
("-fx-cursor : hand");
                   
}
               
}
           
};
            rowFactoryProperty
().addListener(new ChangeListener<Callback<TableView<Person>, TableRow<Person>>>() {
               
@Override
               
public void changed(ObservableValue<? extends Callback<TableView<Person>, TableRow<Person>>> observable, Callback<TableView<Person>, TableRow<Person>> oldValue, Callback<TableView<Person>, TableRow<Person>> newValue) {
                    rowFactoryProperty
().unbind();
                   
if (rowFactoryProperty().get() != rowFactory) {
                        setRowFactory
(rowFactory);
                       
System.out.println("setting new");
                   
}

               
}
           
});

It's a bit hacky but it could work. Be sure to create a new TableRow2 and not TableRow as FilteredTableView is using TableView2 underneath.
Also be sure that the cell styling is not overriding your row styling. In my case, I wanted to modify the background color but it was not working and I suspect the cell styling is higher in term of Z-index that the row style. But the cursor style is working so it proves it can be done.

Marco

unread,
May 6, 2020, 2:33:00 PM5/6/20
to ControlsFX
Thx a lot Samir!
Reply all
Reply to author
Forward
0 new messages