java - How to remove rows in a TableView javafx -


i have been trying add remove button removes selected row in tableview. problem different have found elsewhere. problem lies behind fact in application have used 1 fxml file basis of several different interfaces. when initialize 1 of these , use functionality of remove button removes tableview rows fine , how supposed to. when initialize second interface (still using same fxml , henceforth same variable names) lets me delete items in 2nd tableview , not in first. have idea why is, not know how fix it.

here few methods have tried:

public void removeproject(actionevent event){ int index = projecttable.getselectionmodel().getselectedindex();   if(index >=0){   projecttable.getitems().remove(index);   }else   //show warning   } } 

another different approach:

public void removeproject(actionevent event){ observablelist<project> currentlyselected, allprojects; currentlyselected = projecttable.getselectionmodel().getselectedindex(); allprojects = projecttable.getitems(); currentlyselected.foreach(allprojects::remove); } 

also please keep in mind both of these methods work fine until initialize second tableview. after point value both observablelist<project> currentlyselected , int indexare -1 when trying select row in table isn't recent initialization of interface. sorry if sounds bit confusing bit confusing, if can clear ill add edit later

cheers

edit 1:

here example trying remove table based on interface in currently:

observablelist<project> itemsselected;          switch(counter){         case 1:             itemsselected = projecttable.getselectionmodel().getselecteditems();             itemsselected.foreach(projtablestorage.getproj1()::remove);             break;         case 2:             itemsselected = projecttable.getselectionmodel().getselecteditems();             itemsselected.foreach(projecttablestorage.getproj2()::remove);             break;  

a few things note:

  1. the projecttablestorage.getproj() used store of data in each table, returned value observablelist , use set items of table whenever interface loaded data not lost when swapping between interfaces, perhaps there more efficient ways go it, how did it

  2. there 7 of these interfaces, testing 2 make testing shorter , simpler @ least

edit 2:

loading fxml files:

public anchorpane initlayouts(fxmlloader loader, anchorpane projectlayout, mainlayoutcontroller mainlay) throws ioexception{     loader = new fxmlloader();     loader.setlocation(getclass().getresource("/control/view/projectlayout.fxml"));     loader.setcontroller(mainlay);     projectlayout = (anchorpane) loader.load();     return projectlayout; } 

in mainlayoutcontroller:

public anchorpane loadlayout(anchorpane projectlayout, project project, fxmlloader loader)throws ioexception{     projectlayout = project.initlayouts(loader, projectlayout, this);     return projectlayout; } 

load layout called whenever button pressed

edit 3:

here 'removeprojec' code again

public void removeproject(actionevent event){     observablelist<project> itemsselected, currentproject;      itemsselected = getprojecttable().getselectionmodel().getselecteditems();     itemsselected.foreach(getprojecttable().getitems()::remove);     system.out.println("value of itemsselected : " + itemsselected); } 

and project table storage:

observablelist<project> project1= fxcollections.observablearraylist();  observablelist<project> project2 = fxcollections.observablearraylist();   public void setproject1(project project){     project1.add(project); }  public void setproject2(project project){     project2.add(project); }  public observablelist<project> getproject1(){     return project1; } public observablelist<project> getproject2(){     return project2; } 

and in case getprojecttable method(tried , without annotation):

@fxml public tableview<project> getprojecttable(){     return projecttable; } 

edit 4:

public void createnewprojectlayout(actionevent event) throws ioexception{      if(event.gettarget() == newprojectlayoutbutton1){         projectlayout1 = loadorreloadprojectlayout(newprojectlayoutbutton1, project1, projectlayout1, 1);         settable(counter);      }else if(event.gettarget() == newprojectlayoutbutton2){         projectlayout2 = loadorreloadprojectlayout(newprojectlayoutbutton2, project2, projectlayout2, 2);         settable(counter);      } 

a few things note:

the loadorreload load file first time clicked using loadlayout method mentioned, , reload result of loadlayout next time pressed

the settable used set data stored in table put in table again using observable lists projecttablestorage class


Comments