1

I have a problem with my JavaFX project. There is a moment I can't understand. As far as I understand the following code should be able to handle all scrolling events of a table, which is an instance of TableView

        table.setOnScroll(new EventHandler<ScrollEvent>() {
            @Override
            public void handle(ScrollEvent scrollEvent) {
                System.out.println("Hello!");
                int i = 0;
                int length = table.getItems().size();
                for(Node n: table.lookupAll("TableRow")) {
                    if (n instanceof TableRow) {
                        TableRow row = (TableRow) n;
                        if(table.getItems().get(i).getType() == "fwfx") {                    
                            row.setStyle("-fx-background-color: forestgreen;");
                        }
                        i++;
                    }
                    if(i == length) {
                        break;
                    }
                }
            }
        }
    );

Whenever I launch the application it highlights row correctly only for visible rows. I found it out because

table.lookupAll("TableRow")

returns the set of only 17 nodes for me. although

table.getItems().size()

shows the correct number of rows. If I scroll down the table I see unapproipriate rows highlighted. I'm lost a bit.

So the question is how do I correctly handle the scroll events for my table? I need to process all rows of the table, not only visible.

2
  • I didn't get what you're trying to achieve here. However, the number of rows returned by table.lookupAll(...) is a result of the fact that the TableView class will reuse node for rendering rows. If your table contains a million items, there will still only be enough nodes created to render everything visible on your screen. Therefore, I guess that your 'incorrectly' highlighted rows are reused nodes that do not have their CSS reset (by you).
    – sarcan
    Nov 3, 2012 at 11:01
  • Thank you for reply, @sarcan! I think you are right about the nodes usage. I was trying to avoid it by "refreshing" the node whenever the user scrolls the mouse whill. Probably I "refreshed" the wrong thing... As soon as I find the right solution I will post it here.
    – Oleg
    Nov 3, 2012 at 19:15

3 Answers 3

9

So finally I found the way to handle scrolling events and I would like to share my experience. Using the Scenic View I found that TableView setOnScroll event is fired only when you scroll the mouse wheel with cursor over the column headers. But to be able to handle ScrollEvent when the cursor is above table data (what I needed in my example) one needs to use the EventFilter to be sure. For example, the colde below will handle all scrolling events of TableView's instance

    table.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>() {
            @Override
            public void handle(ScrollEvent scrollEvent) {
               System.out.println("Scrolled.");
            }
     });

Scenic View also gave me a hint about what TableView consists of after

stage.show()

has worked.

Althougth I still have incorrectly highlighted rows)...

1

Listen to scroll bar value changes

for (Node node : dataTable.lookupAll(".scroll-bar"))
{
    if  (node instanceof ScrollBar && ((ScrollBar) node).getOrientation().equals(Orientation.VERTICAL))
    {
        ((ScrollBar) node).valueProperty().addListener(formatBUIScrollChangeEventHandler);
    }
}

This will be triggered in any form of scrolling on the table.

Have been spending hours then finally come out with this idea when solving my own situation.

0

The question is old, but you haven't posted/accepted an answer yet.

So, this schould help: tableView.refresh(). It's available in JavaFX since Java 8u60.

You have to call it inside your ScrollEvent. Also perhaps by sorting and dragging ScrollBar (see my answer here).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.