Oleg
Oleg

Reputation: 457

JavaFX scroll events handling for TableView

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.

Upvotes: 1

Views: 8369

Answers (3)

Ruslan  Gabbazov
Ruslan Gabbazov

Reputation: 772

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).

Upvotes: 0

harytan
harytan

Reputation: 11

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.

Upvotes: 1

Oleg
Oleg

Reputation: 457

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)...

Upvotes: 8

Related Questions