ListView selection listener : ListView « JavaFX « Java






ListView selection listener

 
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.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.Tooltip;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.util.Callback;

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);
  }

}

public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 500, 250, Color.WHITE);
    // create a grid pane
    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(5));
    gridpane.setHgap(10);
    gridpane.setVgap(10);

    ObservableList<Person> leaders = FXCollections.observableArrayList();
    leaders.add(new Person("A", "B", "C"));
    leaders.add(new Person("D", "E", "F"));
    final ListView<Person> leaderListView = new ListView<Person>(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);
                }
              }
            }; // ListCell
            return cell;
          }
        }); // setCellFactory

    gridpane.add(leaderListView, 0, 1);

    leaderListView.getSelectionModel().selectedItemProperty()
        .addListener(new ChangeListener<Person>() {
          public void changed(ObservableValue<? extends Person> observable,
              Person oldValue, Person newValue) {
            System.out.println("selection changed");  
          }
        });

    root.getChildren().add(gridpane);

    primaryStage.setScene(scene);
    primaryStage.show();
  }

}

   
  








Related examples in the same category

1.ListView with data binding
2.Color Rect List Cell
3.List selected Item property
4.Let ListView grow
5.ListView auto filter
6.Display first and last name with tooltip using alias for ListView