Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ListController {
- @FXML
- ListView<Song> listView;
- private ObservableList<Song> obsList;
- public void start(Stage mainStage) throws Exception{
- // create an ObservableList
- // from an ArrayList
- ArrayList<Song> al = new ArrayList<Song>();
- al.add(new Song("Jesus of Suburbia", "Greenday"));
- obsList = FXCollections.observableArrayList(al);
- SortedList<Song> sl = new SortedList<Song>(obsList, new Song.Compare());
- listView.setItems(sl);
- obsList.add(new Song("Birdplane", "Axis of Awesome"));
- // select the first item
- listView.getSelectionModel().select(0);
- // set listener for the items
- listView
- .getSelectionModel()
- .selectedItemProperty()
- .addListener(
- (obs, oldVal, newVal) ->
- showItem(mainStage));
- }
- private void showItem(Stage mainStage) {
- Alert alert =
- new Alert(AlertType.INFORMATION);
- alert.initOwner(mainStage);
- alert.setTitle("List Item");
- alert.setHeaderText(
- "Selected list item properties");
- String content = "Index: " +
- listView.getSelectionModel()
- .getSelectedIndex(); // +
- // "\nValue: " +
- // listView.getSelectionModel()
- // .getSelectedItem();
- alert.setContentText(content);
- alert.showAndWait();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement