java - How to edit default render behaviour of ComboBoxTableCell in JavaFX 2? -


by default comboboxtablecell rendered label when not being edited. want change behaviour rendered combobox time. couldn't find out how.

basically have tableview. of columns of type combobox. want combobox node shown always.

vartypecol.setcellvaluefactory(new propertyvaluefactory<globalvariable, string>("vartype")); vartypecol.setcellfactory(new callback<tablecolumn<globalvariable, string>, tablecell<globalvariable, string>>() {        @override       public tablecell<globalvariable, string> call(tablecolumn<globalvariable, string> p) {             return new comboboxtablecell<globalvariable, string>(fxcollections.observablearraylist("string", "integer"));       } }); 

you need implement own tablecell. like

public class livecomboboxtablecell<s,t> extends tablecell<s, t> {      private final combobox<t> combobox ;      public livecomboboxtablecell(observablelist<t> items) {         this.combobox = new combobox<>(items);          setcontentdisplay(contentdisplay.graphic_only);          combobox.valueproperty().addlistener(new changelistener<t>() {             @override             public void changed(observablevalue<? extends t> obs, t oldvalue, t newvalue) {                 // attempt update property:                 observablevalue<t> property = gettablecolumn().getcellobservablevalue(getindex());                 if (property instanceof writablevalue) {                     ((writablevalue<t>) property).setvalue(newvalue);                 }             }         });     }      @override     public void updateitem(t item, boolean empty) {         super.updateitem(item, empty);         if (empty) {             setgraphic(null);         } else {             combobox.setvalue(item);             setgraphic(combobox);         }     } } 

here sscce:

import javafx.application.application; import javafx.beans.property.simplestringproperty; import javafx.beans.property.stringproperty; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.beans.value.writablevalue; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.combobox; import javafx.scene.control.contentdisplay; import javafx.scene.control.tablecell; import javafx.scene.control.tablecolumn; import javafx.scene.control.tableview; import javafx.scene.control.cell.propertyvaluefactory; import javafx.scene.layout.borderpane; import javafx.stage.stage; import javafx.util.callback;  public class livecomboboxtablecellexample extends application {      @override     public void start(stage primarystage) {         tableview<item> table = new tableview<>();          tablecolumn<item, string> typecol = new tablecolumn<>("type");         typecol.setcellvaluefactory(new propertyvaluefactory<>("type"));         tablecolumn<item, string> valuecol = new tablecolumn<>("value");         valuecol.setcellvaluefactory(new propertyvaluefactory<>("value"));          table.getcolumns().add(typecol);         table.getcolumns().add(valuecol);          typecol.setcellfactory(new callback<tablecolumn<item, string>, tablecell<item, string>>() {              @override             public tablecell<item, string> call(tablecolumn<item, string> param) {                 return new livecomboboxtablecell<>(fxcollections.observablearraylist("string", "integer"));             }          });          (int = 1 ; <= 10; i++) {             table.getitems().add(new item( (i % 2 == 0 ? "string" : "integer"), "item "+i));         }          button checkbutton = new button("run check");         checkbutton.setonaction(new eventhandler<actionevent>() {              @override             public void handle(actionevent event) {                 (item item : table.getitems()) {                     system.out.println(item.gettype() + " : "+item.getvalue());                 }                 system.out.println();             }          });          borderpane root = new borderpane();         root.setcenter(table);         root.setbottom(checkbutton);         scene scene = new scene(root, 600, 400);         primarystage.setscene(scene);         primarystage.show();     }      public static class livecomboboxtablecell<s,t> extends tablecell<s, t> {          private final combobox<t> combobox ;          public livecomboboxtablecell(observablelist<t> items) {             this.combobox = new combobox<>(items);              setcontentdisplay(contentdisplay.graphic_only);              combobox.valueproperty().addlistener(new changelistener<t>() {                 @override                 public void changed(observablevalue<? extends t> obs, t oldvalue, t newvalue) {                     // attempt update property:                     observablevalue<t> property = gettablecolumn().getcellobservablevalue(getindex());                     if (property instanceof writablevalue) {                         ((writablevalue<t>) property).setvalue(newvalue);                     }                 }             });         }          @override         public void updateitem(t item, boolean empty) {             super.updateitem(item, empty);             if (empty) {                 setgraphic(null);             } else {                 combobox.setvalue(item);                 setgraphic(combobox);             }         }     }      public static class item {         private final stringproperty type = new simplestringproperty();         private final stringproperty value = new simplestringproperty();          public item(string type, string value) {             settype(type);             setvalue(value);         }          public final stringproperty typeproperty() {             return this.type;         }          public final string gettype() {             return this.typeproperty().get();         }          public final void settype(final string type) {             this.typeproperty().set(type);         }          public final stringproperty valueproperty() {             return this.value;         }          public final string getvalue() {             return this.valueproperty().get();         }          public final void setvalue(final string value) {             this.valueproperty().set(value);         }       }      public static void main(string[] args) {         launch(args);     } } 

Comments

Popular posts from this blog

jsf - "PropertyNotWritableException: Illegal Syntax for Set Operation" error when setting value in bean -

arrays - Algorithm to find ideal starting spot in a circle -

php - Autoloader issue not returning Class -