Javafx tableview adding image -


@fxml private javafx.scene.control.tableview<user> usertableview;  @fxml private tablecolumn<user, image> isactivecolumn; @fxml private tablecolumn<user, string> usernamecolumn; @fxml private tablecolumn<user, string> taskcolumn; @fxml private tablecolumn<user, string> firstnamecolumn; @fxml private tablecolumn<user, string> lastnamecolumn; @fxml private tablecolumn<user, string> editdeletecolumn;  public static observablelist<user> data;  public void initialize() throws sqlexception {    try {        data = fxcollections.observablearraylist();         addusers();     } catch (sqlexception e) {         e.printstacktrace();     } }  public void clickadduser() throws exception{      fxmlloader fxmlloader = new fxmlloader(getclass().getresource("/fxml/adduser.fxml"));     parent root =  fxmlloader.load();     stage stage = new stage();     stage.setscene(new scene(root));     stage.show(); }  //tablewiev'e kullanici eklenmesi public void addusers() throws sqlexception {     try {         list<user> userlist = main.dbmanager.getuserlist();       if(userlist.size() != 0){             (int i=0; i<userlist.size(); i++){                 user auser = userlist.get(i);                 data.add(auser);             }         }          isactivecolumn.setminwidth(200);         isactivecolumn.setcellfactory(new callback<tablecolumn<user, image>, tablecell<user, image>>() {             public tablecell<user, image> call(tablecolumn<user, image> param) {                  final imageview imageview = new imageview();                 imageview.setfitheight(50);                 imageview.setfitwidth(50);                      tablecell<user, image> cell = new tablecell<user, image>(){                          @override                         protected void updateitem(image item, boolean empty) {                             if(item != null)                               imageview.setimage(new image("images/if_user-green_85406.png"));                         }                      };                     cell.setgraphic(imageview);                     return cell;             }         });         isactivecolumn.setcellvaluefactory(new propertyvaluefactory<user, image>("image"));          usernamecolumn.setminwidth(200);         usernamecolumn.setcellvaluefactory(new propertyvaluefactory<user, string>("ausername"){             @override             public observablevalue<string> call(tablecolumn.celldatafeatures<user, string> param) {                 return new readonlyobjectwrapper(param.getvalue().getusername());             }         });          taskcolumn.setminwidth(200);         taskcolumn.setcellvaluefactory(new propertyvaluefactory<user, string>("atask"){             @override             public observablevalue<string> call(tablecolumn.celldatafeatures<user, string> param) {                 return new readonlyobjectwrapper(param.getvalue().gettask());             }         });          firstnamecolumn.setminwidth(200);         firstnamecolumn.setcellvaluefactory(new propertyvaluefactory<user, string>("afirstname"){             @override             public observablevalue<string> call(tablecolumn.celldatafeatures<user, string> param) {                 return new readonlyobjectwrapper(param.getvalue().getfirstname());             }         });          lastnamecolumn.setminwidth(200);         lastnamecolumn.setcellvaluefactory(new propertyvaluefactory<user, string>("alastname"){             @override             public observablevalue<string> call(tablecolumn.celldatafeatures<user, string> param) {                 return new readonlyobjectwrapper(param.getvalue().getlastname());             }         });          editdeletecolumn.setminwidth(200);         editdeletecolumn.setcellvaluefactory(new propertyvaluefactory<user, string>("editdeletecol"));          usertableview.setitems(data);      } catch (sqlexception e) {         e.printstacktrace();     } }  public observablelist<user> getdata(){     return data; } 

}

i want add image if active or passive. add active or passive state boolean. don't know how before try add static image updateitem method never called. this?

assuming user class defines boolean property active, i.e.

public class user {      private final booleanproperty active = new simplebooleanproperty(false);      public booleanproperty activeproperty() {        return active ;     }      public final boolean isactive() {         return activeproperty().get();     }      public final void setactive(boolean active) {         activeproperty().set(active);     }      // other properties... } 

then should make isactivecol tablecolumn<user, boolean> , set as:

@fxml private tablecolumn<user, boolean> isactivecolumn;  // ...  public void addusers() {      // ...      isactivecolumn.setcellvaluefactory(new propertyvaluefactory<>("active"));      isactivecolumn.setcellfactory(tc -> {         final image activeimage = new image(...);         final image passiveimage = new image(...);         tablecell<user, boolean> cell = new tablecell<user, boolean>() {             private imageview imageview = new imageview();             @override             protected void updateitem(boolean active, boolean empty) {                 super.updateitem(active, empty);                 if (empty) {                     setgraphic(null);                 } else {                     if (active) {                         imageview.setimage(activeimage);                     } else {                         imageview.setimage(passiveimage);                     }                     setgraphic(imageview);                 }             }         };         return cell ;     }); } 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -