ListView selection listener : ListView « JavaFX « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » JavaFX » ListView 




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, 500250, 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, 01);

    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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.