/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ctrl; import java.util.ArrayList; import java.util.Collection; import java.util.List; import javafx.application.Application; import javafx.beans.property.ReadOnlyObjectWrapper; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.stage.Stage; import javafx.stage.StageBuilder; import javafx.util.Callback; /** * * @author bchristi */ public class TableViewScrollTo extends Application { private static double viewWidth = 800.0; // width of visible area private static double viewHeight = 600.0; // height of visible area private static int numColumns = 30; private static int numRows = 150; private enum CellTypes { REGULAR("default cell") { @Override Object createCellObject(final double value) { return value; } }; public final String descr; CellTypes(String descr) { this.descr = descr; } abstract Object createCellObject(final double value); } private static CellTypes cellType = CellTypes.REGULAR; private static TableView> tableView; private Stage stage; public Stage createTestGUI() { // due to baseline metrics TableView isn't intended to be reinstantiated next iteration. if(tableView == null) { tableView = new TableView>(getLines()); tableView.getColumns().addAll(getColumns()); tableView.setPrefSize(viewWidth, viewHeight); tableView.setTableMenuButtonVisible(true); } final Scene scene = new Scene(new Group(tableView), viewWidth, viewHeight); // This causes the table to scroll to the *end* tableView.scrollTo(0); stage = StageBuilder.create() .title("TableView") .scene(scene) .build(); stage.sizeToScene(); return stage; } final static ObservableList> bigData = FXCollections.observableArrayList(); public ObservableList> getLines() { for (int row = bigData.size(); row < numRows; row++) { List line = new ArrayList(); for (int col = 0; col <= numColumns; col++) { double value = (col == 0) ? (double)row : Math.random() * 1000; line.add(cellType.createCellObject(value)); } bigData.add(line); } return bigData; } final static List,Object>> cols = new ArrayList,Object>>(); public Collection,Object>> getColumns() { for (int i = cols.size(); i <= numColumns; i++) { TableColumn,Object> col = new TableColumn,Object>("Col" + i); final int coli = i; col.setCellValueFactory(new Callback,Object>, ObservableValue>() { public ObservableValue call(TableColumn.CellDataFeatures,Object> p) { return new ReadOnlyObjectWrapper(p.getValue().get(coli)); } }); cols.add(col); } return cols; } @Override public void start(Stage primaryStage) { Stage stage = createTestGUI(); stage.show(); } public static void main(String[] args) { Application.launch(TableViewScrollTo.class, args); } }