java - javaFX select mutlipe rows form tableview, put in HashMap -


i have tableview allows user make single selection. want allow user make multiple selections tableview set keys in hashmap, how alter current code allow this. appreciated.

@fxml tableview<run> bookingruntable;  @fxml tablecolumn<run, string> bookingrunname;  @fxml tablecolumn<run, character> bookingrunsize;  @fxml tablecolumn<run, double> bookingrunprice;  @override public void initialize(url location, resourcebundle resources) {     availableruns = fxcollections.observablearraylist(availablerunlist());      bookingrunname.setcellvaluefactory(new propertyvaluefactory<run, string>("runname"));     bookingrunsize.setcellvaluefactory(new propertyvaluefactory<run, character>("runtype"));     bookingrunprice.setcellvaluefactory(new propertyvaluefactory<run, double>("pricepernight"));      bookingruntable.setitems(availableruns);     bookingruntable.getselectionmodel().selecteditemproperty()             .addlistener(new changelistener<run>() {                 // sets selected properties of run detail containers                 @override                 public void changed(                         observablevalue<? extends run> observable,                         run oldvalue, run newvalue) {                     checkrequiredinfo();                 }             });      // want selected rows of tableview put map key, later can assign other values key.      hashmap<integer, integer> bookingdogrunmap = new hashmap<integer, integer>();      bookingdogrunmap.put(bookingruntable.getselectionmodel().getselecteditem().getrunid(), 0); } 

to allow multiple selection, in initialize() method call

bookingruntable.getselectionmodel().setselectionmode(selectionmode.multiple); 

to process selected rows, use getselecteditems() method (note plural) of selectionmodel. example:

for (run run : bookingruntable.getselectionmode().getselecteditems()) {     bookingdogrunmap.put(run.getrunid(), 0); } 

Comments