/* * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. */ package ctrl; import java.util.ArrayList; import java.util.List; import java.util.Random; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.beans.property.LongProperty; import javafx.beans.property.SimpleLongProperty; import javafx.beans.value.ObservableValue; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn.CellDataFeatures; import javafx.scene.control.TableRow; import javafx.scene.control.TableView; import javafx.util.Callback; class SimpleCellData { private LongProperty data = new SimpleLongProperty(); public SimpleCellData(final Long val) { data = new SimpleLongProperty(val); } public final Long getData() { return data.get(); } public final void setData(final Long value){ data.set(value); } public LongProperty dataProperty() { return data; } } public class ShortCellLayout extends Application { public static double VIEW_WIDTH = 800.0; // width of visible area public static double VIEW_HEIGHT = 600.0; // height of visible area public static int COLUMNS = 20; public static int ROWS = 30; // Cell text is placed too low for short row height public static double ROW_HEIGHT = 13.0; // Cell text is centered for tall row heights // public static double ROW_HEIGHT = 48.0; private final Random random = new Random(); private TableView> table = new TableView>(); private final ObservableList> data = FXCollections.observableArrayList(); @Override public void start(Stage stage) { // Create table data for (int i = 0; i < ROWS; i++) { List line = new ArrayList(); for (int j=0; j < COLUMNS; j++) { line.add(new SimpleCellData((long) random.nextInt(999))); } data.add(line); } // Create table columns List, Number>> cols = new ArrayList, Number>>(); for (int i = 0; i < COLUMNS; i++) { TableColumn,Number> col = new TableColumn, Number>("Col-" + i); final int coli = i; col.setCellValueFactory( new Callback, Number>, ObservableValue>() { public ObservableValue call(CellDataFeatures, Number> p) { return p.getValue().get(coli).dataProperty(); } }); col.setCellFactory(new Callback, Number>, TableCell, Number>>() { public TableCell call (TableColumn param) { return new TableCell, Number>() { @Override public void updateItem(Number item, boolean empty) { super.updateItem(item, empty); if (item != null) { setText(item.toString()); } } }; } }); cols.add(col); } // Create the scene Scene scene = new Scene(new Group(), VIEW_WIDTH, VIEW_HEIGHT); stage.setTitle("Short Cell Layout"); table.setRowFactory(new Callback>, TableRow>>() { public TableRow> call(TableView> p) { return new TableRow>() { @Override protected double computePrefHeight(double width) { return ROW_HEIGHT; } }; } }); table.setPrefSize(VIEW_WIDTH-25, VIEW_HEIGHT-50); table.setItems(data); table.getColumns().addAll(cols); ((Group) scene.getRoot()).getChildren().addAll(table); stage.setScene(scene); stage.show(); } public static void main(String[] args) { launch(args); } }