Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Stop TableView small scroll when adding content to bottom of table

JoshuaD:

I have a TableView that has content added to it every 100ms or so, for about 5 or 10 minutes.

While that content is being added, if you aren't at the very top or very bottom of the table, the content scrolls a little bit each time an item is added.

Below is a very simple and runnable example of the problem.

Is there any way to get this table to stay still while content is added, without doing harm to the scrollability / usability otherwise?

TableAutoScroll.java


import java.util.ArrayList;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TableAutoScroll extends Application {

@Override
public void start( Stage stage ) throws Exception {

ObservableList tableContent = FXCollections.observableArrayList( new ArrayList () );

TableColumn firstNameColumn = new TableColumn ( "First Name" );
TableColumn LastNameColumn = new TableColumn ( "Last Name" );
TableColumn addressColumn = new TableColumn ( "Address" );

firstNameColumn.setCellValueFactory( new PropertyValueFactory ( "firstName" ) );
LastNameColumn.setCellValueFactory( new PropertyValueFactory ( "lastName" ) );
addressColumn.setCellValueFactory( new PropertyValueFactory ( "address" ) );

TableView tableView = new TableView ();
tableView.getColumns().addAll( firstNameColumn, LastNameColumn, addressColumn );
tableView.setColumnResizePolicy( TableView.CONSTRAINED_RESIZE_POLICY );
tableView.setItems( tableContent );

for ( int k = 0; k tableContent.add ( new Person ( "Dummy", "Person", k + " Sesame Street" ) );
}

tableView.scrollTo( 200 );

BorderPane pane = new BorderPane( tableView );
stage.setScene( new Scene( pane, 800, 400 ) );
stage.show();

Thread populateThread = new Thread ( () -> {
for ( int k = 0; k Person addMe = new Person ( "Dummy", "Person", k + " Sesame Street" );
Platform.runLater( ()-> tableContent.add( addMe ) );
try {
Thread.sleep( 100 );
} catch (InterruptedException e) {}
}
});

populateThread.setDaemon ( true );
populateThread.start();

}

public static void main ( String[] args ) {
launch ( args );
}
}

Person.java


public class Person {
private String firstName, lastName, address;

public Person ( String firstName, String lastName, String address ) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getAddress() {
return address;
}
}



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered
HERE


This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Stop TableView small scroll when adding content to bottom of table

×

Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription

×