package org.jemmy.client; import java.util.Random; import javafx.application.Application; import javafx.beans.property.StringProperty; import javafx.collections.FXCollections; import javafx.collections.ListChangeListener; import javafx.collections.ListChangeListener.Change; import javafx.collections.ObservableList; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.SelectionMode; import javafx.scene.control.TableColumn; import javafx.scene.control.TableColumn.CellDataFeatures; import javafx.scene.control.TablePosition; import javafx.scene.control.TableView; import javafx.scene.input.MouseEvent; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage; import javafx.util.Callback; /** * * @author Alexander Kouznetsov */ public class TableViewTest extends Application { public static void main(String[] args) { launch(args); } @Override public void start(Stage stage) throws Exception { TableView tableView = new TableView(); ObservableList teamMembers = FXCollections.observableArrayList( new Person("John", "Smith"), new Person("A", "1"), new Person("B", "2"), new Person("C", "3"), new Person("D", "4"), new Person("E", "5"), new Person("F", "6"), new Person("G", "7"), new Person("H", "8"), new Person("I", "9"), new Person("J", "10"), new Person("K", "11"), new Person("L", "12"), new Person("M", "13"), new Person("Victor", "Johnson") ); tableView.setItems(teamMembers); TableColumn firstNameCol = new TableColumn("First Name"); TableColumn lastNameCol = new TableColumn("Last Name"); lastNameCol.setDataRetriever(new Callback, String>() { public String call(CellDataFeatures p) { return ((Person) p.getValue()).getLastName(); } }); firstNameCol.setDataRetriever(new Callback, String>() { public String call(CellDataFeatures p) { return ((Person) p.getValue()).getFirstName(); } }); tableView.getColumns().setAll(firstNameCol, lastNameCol); // tableView.getSelectionModel().setCellSelectionEnabled(true); tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE_INTERVAL_SELECTION); Scene scene = new Scene(tableView); stage.setScene(scene); stage.setVisible(true); tableView.getSelectionModel().getSelectedCells().addListener(new ListChangeListener() { public void onChanged(Change change) { // System.out.println("change = " + change); final ObservableList list = change.getList(); System.out.println("change.getAddedSize() = " + change.getAddedSize()); System.out.println("change.getFrom() = " + change.getFrom()); System.out.println("change.getRemoved() = " + change.getRemoved()); System.out.println("change.getRemovedSize() = " + change.getRemovedSize()); System.out.println("change.getTo() = " + change.getTo()); if (list.isEmpty()) { System.out.println("nothing selected"); } else { final TablePosition cell = (TablePosition) list.get(list.size() - 1); System.out.println("selected = " + cell.getColumn() + ", " + cell.getRow()); } } }); final ObservableList list = FXCollections.observableArrayList(); list.addListener(new ListChangeListener() { public void onChanged(Change change) { // System.out.println("change = " + change); final ObservableList list = change.getList(); if (list.isEmpty()) { System.out.println("empty list"); } else { System.out.println("list = " + list.get(0)); } } }); final Random random = new Random(); Rectangle clickMe = new Rectangle(100, 100, Color.RED); clickMe.setOnMouseClicked(new EventHandler() { public void handle(MouseEvent t) { if (t.isAltDown()) { list.setAll(random.nextInt()); } else { list.setAll(10); } } }); Group root = new Group(clickMe); Scene infoScene = new Scene(root); Stage infoStage = new Stage(); infoStage.setTitle("Info"); infoStage.setScene(infoScene); infoStage.setVisible(true); } } final class Person { private StringProperty firstName = new StringProperty(); private StringProperty lastName = new StringProperty(); public void setFirstName(String value) { firstName.set(value); } public String getFirstName() { return firstName.get(); } public StringProperty firstNameProperty() { return firstName; } public void setLastName(String value) { lastName.set(value); } public String getLastName() { return lastName.get(); } public StringProperty lastNameProperty() { return lastName; } public Person(String firstName, String lastName) { setFirstName(firstName); setLastName(lastName); } }