Java examples for JavaFX:Table
create JavaFX Table View
/*/*from w w w. j a v a2 s . com*/ * Copyright (c) 2008, 2013, Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. * * This file is available and licensed under the following license: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the distribution. * - Neither the name of Oracle Corporation nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import javafx.beans.binding.ObjectBinding; import javafx.beans.binding.StringBinding; import javafx.beans.property.BooleanProperty; import javafx.beans.property.ReadOnlyObjectWrapper; import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.collections.SetChangeListener; import javafx.event.EventHandler; import javafx.scene.Node; import javafx.scene.control.Label; import javafx.scene.control.SelectionMode; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumnBase; import javafx.scene.control.TableView; import javafx.scene.control.cell.CheckBoxTableCell; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.paint.Color; import javafx.scene.paint.Paint; import javafx.scene.shape.Rectangle; import javafx.util.Callback; public class Main{ private static ObservableList<Person> data = FXCollections .<Person> observableArrayList(); static TableView createTableView(int width, boolean rowSelection) { TableColumn<Person, String> firstNameCol, lastNameCol, nameCol, emailCol, countryCol; TableColumn<Person, Boolean> invitedCol; // Columns firstNameCol = new TableColumn<Person, String>(); firstNameCol.setText("First"); // Rectangle sortNode = new Rectangle(10, 10, Color.RED); // sortNode.fillProperty().bind(new ObjectBinding<Paint>() { // { bind(firstNameCol.sortTypeProperty()); } // @Override protected Paint computeValue() { // switch (firstNameCol.getSortType()) { // case ASCENDING: return Color.GREEN; // case DESCENDING: return Color.RED; // default: return Color.BLACK; // } // } // }); // firstNameCol.setSortNode(sortNode); firstNameCol .setCellValueFactory(new PropertyValueFactory<Person, String>( "firstName")); firstNameCol .setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Person, String>>() { @Override public void handle( TableColumn.CellEditEvent<Person, String> t) { System.out.println("Edit commit event: " + t.getNewValue()); } }); final Node graphic1 = new ImageView(new Image( "file:src/helloworld/about_16.png")); lastNameCol = new TableColumn<Person, String>(); lastNameCol.setGraphic(graphic1); lastNameCol.setText("Last"); lastNameCol.setSortType(TableColumn.SortType.DESCENDING); lastNameCol .setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person, String>, ObservableValue<String>>() { public ObservableValue<String> call( TableColumn.CellDataFeatures<Person, String> p) { return p.getValue().lastNameProperty(); } }); nameCol = new TableColumn<Person, String>(); nameCol.setText("Name"); nameCol.getColumns().addAll(firstNameCol, lastNameCol); emailCol = new TableColumn<Person, String>(); emailCol.setText("Email"); emailCol.setMinWidth(200); emailCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person, String>, ObservableValue<String>>() { public ObservableValue<String> call( TableColumn.CellDataFeatures<Person, String> p) { return p.getValue().emailProperty(); } }); countryCol = new TableColumn<Person, String>(); countryCol.setText("Country"); countryCol .setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person, String>, ObservableValue<String>>() { public ObservableValue<String> call( TableColumn.CellDataFeatures<Person, String> p) { return new ReadOnlyObjectWrapper<String>( "New Zealand"); } }); // Test case for RT-28410 MODENA: can't make tree/table cell factories change color based // on background when setGraphic(...) is used countryCol .setCellFactory(new Callback<TableColumn<Person, String>, TableCell<Person, String>>() { @Override public TableCell<Person, String> call( TableColumn<Person, String> param) { final Label label = new Label(); label.setStyle("-fx-font-family: 'Times New Roman';" + "-fx-font-size: 0.8em;" + "-fx-text-fill: ladder(-fx-background, yellow 49%, red 50%);"); TableCell cell = new TableCell() { @Override protected void updateItem(Object item, boolean empty) { label.setText(empty ? null : item .toString()); } }; cell.setGraphic(label); return cell; } }); invitedCol = new TableColumn<Person, Boolean>(); invitedCol.setText("Invited"); invitedCol.setPrefWidth(55); invitedCol.setMaxWidth(55); invitedCol.setCellValueFactory(new PropertyValueFactory("invited")); invitedCol .setCellFactory(new Callback<TableColumn<Person, Boolean>, TableCell<Person, Boolean>>() { public TableCell<Person, Boolean> call( TableColumn<Person, Boolean> p) { return new CheckBoxTableCell<Person, Boolean>(); } }); TableView<Person> tableView = new TableView<Person>(); tableView.getSelectionModel().setSelectionMode( SelectionMode.MULTIPLE); tableView.getSelectionModel() .setCellSelectionEnabled(!rowSelection); tableView.setTableMenuButtonVisible(true); tableView.setItems(data); tableView.getColumns().addAll(invitedCol, nameCol, emailCol, countryCol); tableView.setPrefSize(width, 300); if (rowSelection) { tableView.getSelectionModel().selectRange(2, 5); } else { tableView.getSelectionModel().select(2, emailCol); tableView.getSelectionModel().select(3, firstNameCol); tableView.getSelectionModel().select(3, countryCol); tableView.getSelectionModel().select(4, lastNameCol); } tableView.getSortOrder().addAll(firstNameCol, lastNameCol, emailCol, countryCol); return tableView; } }