简体   繁体   中英

JavaFX how to add ChangeListener to auto scroll TableView

I have a TableView:

@FXML
    private TableView<ThreadInfo> threadsTable;

Class ThreadInfo:

public class ThreadInfo {

    private StringProperty threadFrom;

    private StringProperty threadDate;

    private StringProperty threadStatus;

    private StringProperty threadTo;

// getters, setters

}

I'm trying to add ChangeListener to auto scroll TableView down every time new ThreadInfo object is addedd to my TableView. What I tried:

threadsTable.getItems().addListener(
                new ListChangeListener<ThreadInfo>() {
                    @Override
                    public void onChanged(
                            javafx.collections.ListChangeListener.Change<? extends ThreadInfo> arg0) {
                        threadsTable.scrollTo(threadsTable.getItems().size());
                    }

                });

It has no compilation errors, but doesn't work. I also tried with ChangeListener, but I get: The method addListener(ListChangeListener) in the type ObservableList is not applicable for the arguments (new ChangeListener(){}).

I've found my problem. Both methods do the job:

threadsTable.getItems().addListener(
                new ListChangeListener<ThreadInfo>() {
                    @Override
                    public void onChanged(
                            javafx.collections.ListChangeListener.Change<? extends ThreadInfo> arg0) {
                        threadsTable.scrollTo(threadsTable.getItems().size());
                    }

                });

And:

threadsTable.accessibleRoleProperty().addListener(
            new ChangeListener<Object>() {

                @Override
            public void changed(ObservableValue<? extends Object> arg0,
                    Object arg1, Object arg2) {
                threadsTable.scrollTo(threadsTable.getItems().size()-1);
            }
        });

The problem is that I'm putting about 1k or more entries to my TableView and it's laggy. On smaller lists it works perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM