package test; import java.util.Date; import javafx.application.Application; import javafx.beans.property.SimpleObjectProperty; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn.SortType; import javafx.scene.control.TableView; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.util.Callback; import com.celertech.web.orderrouting.client.formatters.table.cell.IntegerFormatCell; import com.celertech.web.orderrouting.client.util.ResourceLoader; public class TableApplication extends Application { private TableView orderTable = new TableView(); ObservableList data = FXCollections.observableArrayList(); @Override public void start(final Stage stage) throws Exception { final VBox rootPane = new VBox(); rootPane.setSpacing(10); rootPane.setPrefSize(800, 600); TableColumn clOrderIdCol = new TableColumn("Id"); clOrderIdCol.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.ORDERID_PROPERTY)); clOrderIdCol.setSortable(true); clOrderIdCol.setPrefWidth(130); TableColumn securityCodeCol = new TableColumn("Security"); securityCodeCol.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.SECURITY_CODE_PROPERTY)); securityCodeCol.setSortable(true); securityCodeCol.setPrefWidth(70); TableColumn StringCol = new TableColumn("String"); StringCol.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.String_PROPERTY)); StringCol.setSortable(true); StringCol.setPrefWidth(65); TableColumn priceCol = new TableColumn("Price"); priceCol.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.PRICE_PROPERTY)); priceCol.setSortable(true); priceCol.setPrefWidth(81); TableColumn qtyCol = new TableColumn("Qty"); qtyCol.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.QTY_PROPERTY)); qtyCol.setSortable(true); qtyCol.setPrefWidth(75); TableColumn accountCol = new TableColumn("Account"); accountCol.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.ACCOUNT_PROPERTY)); accountCol.setSortable(true); accountCol.setPrefWidth(100); TableColumn test1Col = new TableColumn("String"); test1Col.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.String_PROPERTY)); test1Col.setSortable(true); test1Col.setPrefWidth(65); TableColumn test2ol = new TableColumn("Price"); test2ol.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.PRICE_PROPERTY)); test2ol.setSortable(true); test2ol.setPrefWidth(81); TableColumn test3ol = new TableColumn("Qty"); test3ol.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.QTY_PROPERTY)); test3ol.setSortable(true); test3ol.setPrefWidth(75); TableColumn test4ol = new TableColumn("Account"); test4ol.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.ACCOUNT_PROPERTY)); test4ol.setSortable(true); test4ol.setPrefWidth(100); TableColumn createdCol = new TableColumn("Updated"); createdCol.setCellValueFactory(new PropertyValueFactory(OrderDataTableNewRow.UPDATED_PROPERTY)); createdCol.setSortable(true); createdCol.setMinWidth(190); createdCol.setSortType(SortType.DESCENDING); // Setup all the renders Callback, TableCell> defaultStringRenderFactory = new Callback, TableCell>() { public TableCell call(TableColumn p) { return new StringFormatCell(Pos.CENTER); } }; clOrderIdCol.setCellFactory(defaultStringRenderFactory); securityCodeCol.setCellFactory(defaultStringRenderFactory); StringCol.setCellFactory(defaultStringRenderFactory); accountCol.setCellFactory(defaultStringRenderFactory); this.orderTable.setItems(data); this.orderTable.setTableMenuButtonVisible(true); ObservableList> columns = this.orderTable.getColumns(); columns.addAll(clOrderIdCol, securityCodeCol, StringCol, priceCol, qtyCol, accountCol, test1Col, test2ol, test3ol, test4ol, createdCol); this.orderTable.prefWidthProperty().bind(rootPane.widthProperty()); this.orderTable.prefHeightProperty().bind(rootPane.heightProperty()); this.orderTable.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); rootPane.getChildren().addAll(orderTable); loadData(); Scene scene = new Scene(rootPane, 400, 400, Color.WHITE); stage.setResizable(true); stage.setScene(scene); stage.show(); } private final void loadData() { for (int i = 0; i < 10; i++) { OrderDataTableNewRow orderDataTableNewRow = new OrderDataTableNewRow("ID:"+i, "TEST", "TEST2", 92.22, i, "DEMO_ACCOUNT"); data.add(orderDataTableNewRow); } } public static void main(String[] args) { Application.launch(TableApplication.class, args); } public class OrderDataTableNewRow { public static final String ORDERID_PROPERTY = "orderId"; public static final String SECURITY_CODE_PROPERTY = "code"; public static final String String_PROPERTY = "String"; public static final String PRICE_PROPERTY = "price"; public static final String QTY_PROPERTY = "qty"; public static final String ACCOUNT_PROPERTY = "account"; public static final String UPDATED_PROPERTY = "updated"; private final String orderId; private final String code; private final String String; private final String account; private final double price; private final int qty; private final SimpleObjectProperty updated = new SimpleObjectProperty(); /** * Creates a new instance of {@link OrderDataTableNewRow}. */ public OrderDataTableNewRow(String orderId, String code, String String, double price, int qty, String account) { this.orderId = orderId; this.code = code; this.String = String; this.account = account; this.price = price; this.qty = qty; } public void setUpdated(Date date) { this.updated.set(date); } /** * @return the orderId */ public final String getOrderId() { return this.orderId; } /** * @return the code */ public final String getCode() { return this.code; } /** * @return the String */ public final String getString() { return this.String; } /** * @return the account */ public final String getAccount() { return this.account; } /** * @return the price */ public final double getPrice() { return this.price; } /** * @return the qty */ public final int getQty() { return this.qty; } /** * @return the updated */ public final SimpleObjectProperty getUpdated() { return this.updated; } /** * @return the created */ public final SimpleObjectProperty updatedProperty() { return this.updated; } } public class StringFormatCell extends TableCell { private final Pos alignment; private final String cssFormat; /** * Creates a new instance of {@link IntegerFormatCell}. */ public StringFormatCell(Pos alignment) { this(null, alignment); } /** * Creates a new instance of {@link IntegerFormatCell}. */ public StringFormatCell(String cssFormat, Pos alignment) { this.cssFormat = cssFormat; this.alignment = alignment; setId(this.cssFormat); } /** * {@inheritDoc} * @see javafx.scene.control.Cell#updateItem(java.lang.Object, boolean) */ @Override protected void updateItem(String item, boolean empty) { if(item == null) { return; } // calling super here is very important - don't skip this! super.updateItem(item, empty); setText(item); setAlignment(alignment); } } }