我有一个TableView
,每100毫秒左右添加一次内容,大约5或10分钟。
在添加该内容时,如果您不在表的顶部或底部,则每次添加项时内容都会滚动一点。
下面是一个非常简单且可运行的问题示例。
有没有办法让这个表在添加内容时保持静止,否则不会损害可滚动性/可用性?
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<Person> tableContent = FXCollections.observableArrayList( new ArrayList <Person>() );
TableColumn <Person, String> firstNameColumn = new TableColumn <> ( "First Name" );
TableColumn <Person, String> LastNameColumn = new TableColumn <> ( "Last Name" );
TableColumn <Person, String> addressColumn = new TableColumn <> ( "Address" );
firstNameColumn.setCellValueFactory( new PropertyValueFactory <Person, String>( "firstName" ) );
LastNameColumn.setCellValueFactory( new PropertyValueFactory <Person, String>( "lastName" ) );
addressColumn.setCellValueFactory( new PropertyValueFactory <Person, String>( "address" ) );
TableView <Person> tableView = new TableView <> ();
tableView.getColumns().addAll( firstNameColumn, LastNameColumn, addressColumn );
tableView.setColumnResizePolicy( TableView.CONSTRAINED_RESIZE_POLICY );
tableView.setItems( tableContent );
for ( int k = 0; k < 500; 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 < 10000; 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;
}
}