JavaFX ListView set cell factory
import javafx.application.Application; 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.geometry.HPos; import javafx.geometry.Insets; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.ListCell; import javafx.scene.control.ListView; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.Tooltip; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.layout.GridPane; import javafx.scene.paint.Color; import javafx.stage.Stage; import javafx.util.Callback; public class Main extends Application { public static void main(String[] args) { Application.launch(Main.class, args); }//w ww .j a va 2 s . co m @Override public void start(Stage primaryStage) { primaryStage.setTitle("Working with Tables"); Group root = new Group(); Scene scene = new Scene(root, 500, 250, Color.WHITE); GridPane gridpane = new GridPane(); gridpane.setPadding(new Insets(5)); gridpane.setHgap(10); gridpane.setVgap(10); Label candidatesLbl = new Label("Boss"); GridPane.setHalignment(candidatesLbl, HPos.CENTER); gridpane.add(candidatesLbl, 0, 0); ObservableList<Person> leaders = getPeople(); final ListView<Person> leaderListView = new ListView<>(leaders); leaderListView.setPrefWidth(150); leaderListView.setPrefHeight(150); leaderListView.setCellFactory(new Callback<ListView<Person>, ListCell<Person>>() { public ListCell<Person> call(ListView<Person> param) { final Label leadLbl = new Label(); final Tooltip tooltip = new Tooltip(); final ListCell<Person> cell = new ListCell<Person>() { @Override public void updateItem(Person item, boolean empty) { super.updateItem(item, empty); if (item != null) { leadLbl.setText(item.getAliasName()); setText(item.getFirstName() + " " + item.getLastName()); tooltip.setText(item.getAliasName()); setTooltip(tooltip); } } }; return cell; } }); gridpane.add(leaderListView, 0, 1); Label emplLbl = new Label("Employees"); gridpane.add(emplLbl, 2, 0); GridPane.setHalignment(emplLbl, HPos.CENTER); final TableView<Person> employeeTableView = new TableView<>(); employeeTableView.setPrefWidth(300); final ObservableList<Person> teamMembers = FXCollections.observableArrayList(); employeeTableView.setItems(teamMembers); TableColumn<Person, String> aliasNameCol = new TableColumn<>("Alias"); aliasNameCol.setEditable(true); aliasNameCol.setCellValueFactory(new PropertyValueFactory("aliasName")); aliasNameCol.setPrefWidth(employeeTableView.getPrefWidth() / 3); TableColumn<Person, String> firstNameCol = new TableColumn<>("First Name"); firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName")); firstNameCol.setPrefWidth(employeeTableView.getPrefWidth() / 3); TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name"); lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName")); lastNameCol.setPrefWidth(employeeTableView.getPrefWidth() / 3); employeeTableView.getColumns().setAll(aliasNameCol, firstNameCol, lastNameCol); gridpane.add(employeeTableView, 2, 1); // selection listening leaderListView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Person>() { public void changed(ObservableValue<? extends Person> observable, Person oldValue, Person newValue) { if (observable != null && observable.getValue() != null) { teamMembers.clear(); teamMembers.addAll(observable.getValue().employeesProperty()); } } }); root.getChildren().add(gridpane); primaryStage.setScene(scene); primaryStage.show(); } private ObservableList<Person> getPeople() { ObservableList<Person> people = FXCollections.<Person>observableArrayList(); Person tester = new Person("Tester 1", "A", "A"); tester.employeesProperty().add(new Person("Tester 2", "B", "B")); tester.employeesProperty().add(new Person("Tester 3", "C", "C")); tester.employeesProperty().add(new Person("Tester 4", "D", "D")); Person dev = new Person("Developer 1", "F", "F"); dev.employeesProperty().add(new Person("Developer 2", "G", "G")); dev.employeesProperty().add(new Person("Developer 3", "H", "H")); dev.employeesProperty().add(new Person("Developer 4", "I", "I")); Person manager = new Person("Manager 1", "J", "Gennick"); manager.employeesProperty().add(new Person("Manager 2", "K", "K")); manager.employeesProperty().add(new Person("Manager 3", "L", "L")); manager.employeesProperty().add(new Person("Manager 4", "M", "M")); manager.employeesProperty().add(new Person("Manager 5", "N", "N")); manager.employeesProperty().add(new Person("Manager 6", "O", "O")); people.add(tester); people.add(dev); people.add(manager); return people; } } public class Person { private StringProperty aliasName; private StringProperty firstName; private StringProperty lastName; private ObservableList<Person> employees = FXCollections.observableArrayList(); public final void setAliasName(String value) { aliasNameProperty().set(value); } public final String getAliasName() { return aliasNameProperty().get(); } public StringProperty aliasNameProperty() { if (aliasName == null) { aliasName = new SimpleStringProperty(); } return aliasName; } public final void setFirstName(String value) { firstNameProperty().set(value); } public final String getFirstName() { return firstNameProperty().get(); } public StringProperty firstNameProperty() { if (firstName == null) { firstName = new SimpleStringProperty(); } return firstName; } public final void setLastName(String value) { lastNameProperty().set(value); } public final String getLastName() { return lastNameProperty().get(); } public StringProperty lastNameProperty() { if (lastName == null) { lastName = new SimpleStringProperty(); } return lastName; } public ObservableList<Person> employeesProperty() { return employees; } public Person(String alias, String firstName, String lastName) { setAliasName(alias); setFirstName(firstName); setLastName(lastName); } }