listener de la selection dans une listview

Solutions on MaxInterview for listener de la selection dans une listview by the best coders in the world

showing results for - "listener de la selection dans une listview"
Alexander
08 Jan 2017
1 
2import javafx.application.Application;
3import javafx.beans.property.SimpleStringProperty;
4import javafx.beans.property.StringProperty;
5import javafx.beans.value.ChangeListener;
6import javafx.beans.value.ObservableValue;
7import javafx.collections.FXCollections;
8import javafx.collections.ObservableList;
9import javafx.geometry.Insets;
10import javafx.scene.Group;
11import javafx.scene.Scene;
12import javafx.scene.control.Label;
13import javafx.scene.control.ListCell;
14import javafx.scene.control.ListView;
15import javafx.scene.control.Tooltip;
16import javafx.scene.layout.GridPane;
17import javafx.scene.paint.Color;
18import javafx.stage.Stage;
19import javafx.util.Callback;
20
21class Person {
22  private StringProperty aliasName;
23  private StringProperty firstName;
24  private StringProperty lastName;
25  private ObservableList<Person> employees = FXCollections
26      .observableArrayList();
27
28  public final void setAliasName(String value) {
29    aliasNameProperty().set(value);
30  }
31
32  public final String getAliasName() {
33    return aliasNameProperty().get();
34  }
35
36  public StringProperty aliasNameProperty() {
37    if (aliasName == null) {
38      aliasName = new SimpleStringProperty();
39    }
40    return aliasName;
41  }
42
43  public final void setFirstName(String value) {
44    firstNameProperty().set(value);
45  }
46
47  public final String getFirstName() {
48    return firstNameProperty().get();
49  }
50
51  public StringProperty firstNameProperty() {
52    if (firstName == null) {
53      firstName = new SimpleStringProperty();
54    }
55    return firstName;
56  }
57
58  public final void setLastName(String value) {
59    lastNameProperty().set(value);
60  }
61
62  public final String getLastName() {
63    return lastNameProperty().get();
64  }
65
66  public StringProperty lastNameProperty() {
67    if (lastName == null) {
68      lastName = new SimpleStringProperty();
69    }
70    return lastName;
71  }
72
73  public ObservableList<Person> employeesProperty() {
74    return employees;
75  }
76
77  public Person(String alias, String firstName, String lastName) {
78    setAliasName(alias);
79    setFirstName(firstName);
80    setLastName(lastName);
81  }
82
83}
84
85public class Main extends Application {
86  public static void main(String[] args) {
87    Application.launch(args);
88  }
89
90  @Override
91  public void start(Stage primaryStage) {
92    primaryStage.setTitle("");
93    Group root = new Group();
94    Scene scene = new Scene(root, 500, 250, Color.WHITE);
95    // create a grid pane
96    GridPane gridpane = new GridPane();
97    gridpane.setPadding(new Insets(5));
98    gridpane.setHgap(10);
99    gridpane.setVgap(10);
100
101    ObservableList<Person> leaders = FXCollections.observableArrayList();
102    leaders.add(new Person("A", "B", "C"));
103    leaders.add(new Person("D", "E", "F"));
104    final ListView<Person> leaderListView = new ListView<Person>(leaders);
105    leaderListView.setPrefWidth(150);
106    leaderListView.setPrefHeight(150);
107
108    // 
109    leaderListView
110        .setCellFactory(new Callback<ListView<Person>, ListCell<Person>>() {
111
112          public ListCell<Person> call(ListView<Person> param) {
113            final Label leadLbl = new Label();
114            final Tooltip tooltip = new Tooltip();
115            final ListCell<Person> cell = new ListCell<Person>() {
116              @Override
117              public void updateItem(Person item, boolean empty) {
118                super.updateItem(item, empty);
119                if (item != null) {
120                  leadLbl.setText(item.getAliasName());
121                  setText(item.getFirstName() + " " + item.getLastName());
122                  tooltip.setText(item.getAliasName());
123                  setTooltip(tooltip);
124                }
125              }
126            }; // ListCell
127            return cell;
128          }
129        }); // setCellFactory
130
131    gridpane.add(leaderListView, 0, 1);
132
133    leaderListView.getSelectionModel().selectedItemProperty()
134        .addListener(new ChangeListener<Person>() {
135          public void changed(ObservableValue<? extends Person> observable,
136              Person oldValue, Person newValue) {
137            System.out.println("selection changed");  
138          }
139        });
140
141    root.getChildren().add(gridpane);
142
143    primaryStage.setScene(scene);
144    primaryStage.show();
145  }
146
147}
148
149   
150  
151