Noufal Panolan
Noufal Panolan

Reputation: 1367

javafx tableview auto scroll to the last

I want to show the last item added in the table view when appears a scroll bar. Is there any way for the same and it will be grateful if can.

I have checked scroll pane with horizontal and vertical setValue() property. Is there any similar way for the table view scroll bar?

Upvotes: 7

Views: 13639

Answers (2)

Vertex
Vertex

Reputation: 2712

Here is a full generic solution:

public static <S> void addAutoScroll(final TableView<S> view) {
    if (view == null) {
        throw new NullPointerException();
    }

    view.getItems().addListener((ListChangeListener<S>) (c -> {
        c.next();
        final int size = view.getItems().size();
        if (size > 0) {
            view.scrollTo(size - 1);
        }
    }));
}

Upvotes: 5

Uluk Biy
Uluk Biy

Reputation: 49185

Check the TableView.scrollTo() out.

Upvotes: 14

Related Questions