I have two buttons. One that clears the table and one that fills it with 100 rows. I press the fill button followed by clear and repeat this four times.
Filling the table gets increasingly slow each time. On the forth fill the application hangs. I'm running with latest java version on Windows XP 32-bit.
package javafxapplication1;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.TableView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;
public class JavaFXApplication1 extends Application {
private TableView<Person> _tableView;
private ObservableList<Person> _items;
public static void main(String[] args) {
Application.launch(JavaFXApplication1.class, args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Table view test");
Group root = new Group();
Scene scene = new Scene(root, 400, 400, Color.BEIGE);
Button btnClear = new Button();
btnClear.setLayoutX(300);
btnClear.setLayoutY(80);
btnClear.setText("Clear");
Button btnFill = new Button();
btnFill.setLayoutX(300);
btnFill.setLayoutY(130);
btnFill.setText("Fill");
btnClear.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
btnClearPressed();
}
});
btnFill.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
btnFillPressed();
}
});
root.getChildren().add(btnClear);
root.getChildren().add(btnFill);
primaryStage.setScene(scene);
primaryStage.setVisible(true);
_items = FXCollections.observableArrayList ();
_tableView = new TableView<Person>();
TableColumn<Integer> col1 = new TableColumn<Integer>("Number");
TableColumn<String> col2 = new TableColumn<String>("First Name");
TableColumn<String> col3 = new TableColumn<String>("Last Name");
col1.setDataRetriever(new Callback<CellDataFeatures<Integer>, Integer>() {
public Integer call(CellDataFeatures<Integer> p) {
return ((Person)p.getValue()).getNumber();
}
});
col2.setDataRetriever(new Callback<CellDataFeatures<String>, String>() {
public String call(CellDataFeatures<String> p) {
return ((Person)p.getValue()).getFirstName();
}
});
col3.setDataRetriever(new Callback<CellDataFeatures<String>, String>() {
public String call(CellDataFeatures<String> p) {
return ((Person)p.getValue()).getLastName();
}
});
_tableView.getColumns().setAll(col1, col2, col3);
root.getChildren().add(_tableView);
}
public void btnClearPressed(){
_items.clear();
_tableView.setItems(_items);
}
public void btnFillPressed(){
_items.clear();
for(int i=0; i < 100; i++)
{
Person p = new Person();
p.setFirstName("Stefan");
p.setLastName("Holm");
p.setNumber(i+1);
_items.add(p);
}
_tableView.setItems(_items);
}
}
package javafxapplication1;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.StringProperty;
public class Person {
private IntegerProperty number;
public void setNumber(Integer value) { numberProperty().set(value); }
public Integer getNumber() { return number.get(); }
public IntegerProperty numberProperty() {
if (number == null) number = new IntegerProperty();
return number;
}
private StringProperty firstName;
public void setFirstName(String value) { firstNameProperty().set(value); }
public String getFirstName() { return firstName.get(); }
public StringProperty firstNameProperty() {
if (firstName == null) firstName = new StringProperty();
return firstName;
}
private StringProperty lastName;
public void setLastName(String value) { lastNameProperty().set(value); }
public String getLastName() { return lastName.get(); }
public StringProperty lastNameProperty() {
if (lastName == null) lastName = new StringProperty();
return lastName;
}
}