Advertisement
Guest User

ListController.java

a guest
Feb 10th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. public class ListController {
  2.     @FXML        
  3.        ListView<Song> listView;                
  4.  
  5.        private ObservableList<Song> obsList;              
  6.      
  7.        public void start(Stage mainStage) throws Exception{                
  8.           // create an ObservableList
  9.           // from an ArrayList              
  10.            ArrayList<Song> al = new ArrayList<Song>();
  11.            al.add(new Song("Jesus of Suburbia", "Greenday"));
  12.           obsList = FXCollections.observableArrayList(al);
  13.          
  14.           SortedList<Song> sl = new SortedList<Song>(obsList, new Song.Compare());
  15.           listView.setItems(sl);
  16.           obsList.add(new Song("Birdplane", "Axis of Awesome"));
  17.          
  18.           // select the first item
  19.           listView.getSelectionModel().select(0);
  20.  
  21.           // set listener for the items
  22.           listView
  23.           .getSelectionModel()
  24.           .selectedItemProperty()
  25.           .addListener(
  26.                   (obs, oldVal, newVal) ->
  27.                   showItem(mainStage));
  28.  
  29.        }
  30.  
  31.        private void showItem(Stage mainStage) {                
  32.            Alert alert =
  33.                    new Alert(AlertType.INFORMATION);           
  34.  
  35.        
  36.            alert.initOwner(mainStage);
  37.            alert.setTitle("List Item");
  38.            alert.setHeaderText(
  39.                    "Selected list item properties");
  40.  
  41.            String content = "Index: " +
  42.                    listView.getSelectionModel()
  43.            .getSelectedIndex(); // +
  44. //         "\nValue: " +
  45. //         listView.getSelectionModel()
  46. //         .getSelectedItem();
  47.            
  48.            
  49.  
  50.            alert.setContentText(content);
  51.            alert.showAndWait();
  52.        }
  53.  
  54.  
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement