javafx - How to make a TableView Column as LocalDate and set it's value only via FXML? -
how can fill localdate values column in fxml only?
somewhere in controller class:
protected void addperson(actionevent event) { observablelist<person> data = tableview.getitems(); data.add(new person(firstnamefield.gettext(),lastnamefield.gettext(),emailfield.gettext(),checkbox.isselected(),integer.parseint(numberfield.gettext()),localdate.now())); firstnamefield.settext(""); lastnamefield.settext(""); emailfield.settext(""); checkbox.setselected(false); numberfield.settext("0"); datefield.settext(""+localdate.now()); }
person class:
public class person { private final simplestringproperty firstname = new simplestringproperty(""); private final simplestringproperty lastname = new simplestringproperty(""); private final simplestringproperty email = new simplestringproperty(""); private final simplebooleanproperty checked = new simplebooleanproperty(false); private final simpleintegerproperty alter = new simpleintegerproperty(); private final objectproperty<localdate> date;
...
fxml: ...
<tablecolumn prefwidth="75.0" text="date"> <cellvaluefactory> <propertyvaluefactory property="date" /> </cellvaluefactory> </tablecolumn> <tablecolumn prefwidth="75.0" text="g" /> </columns> <items> <fxcollections fx:factory="observablearraylist"> <person alter="1" checked="false" email="tester1.test@test.com" firstname="tester1" lastname="test"/> </fxcollections> </items>
i tried:
<person alter="1" checked="false" email="tester1.test@test.com" firstname="tester1" lastname="test" date="2016.01.01"/>
but javafx.fxml.loadexception:
caused by: java.lang.illegalargumentexception: unable coerce 2016.01.01 class java.time.localdate. @ com.sun.javafx.fxml.beanadapter.coerce(beanadapter.java:496) @ com.sun.javafx.fxml.beanadapter.put(beanadapter.java:258) @ com.sun.javafx.fxml.beanadapter.put(beanadapter.java:54) @ javafx.fxml.fxmlloader$element.applyproperty(fxmlloader.java:512) @ javafx.fxml.fxmlloader$element.processvalue(fxmlloader.java:363) @ javafx.fxml.fxmlloader$element.processpropertyattribute(fxmlloader.java:325) @ javafx.fxml.fxmlloader$element.processinstancepropertyattributes(fxmlloader.java:235) @ javafx.fxml.fxmlloader$valueelement.processendelement(fxmlloader.java:767) @ javafx.fxml.fxmlloader.processendelement(fxmlloader.java:2823) @ javafx.fxml.fxmlloader.loadimpl(fxmlloader.java:2532) ... 13 more
fxmlloader
cannot initialize localdate
string
leaves 2 options:
using valueof
+ fx:value
implement method public static localdate valueof(string)
in class
public final class util { private util() { } private static final datetimeformatter date_format = datetimeformatter.ofpattern("yyyy.mm.dd"); public static localdate valueof(string val) { return localdate.parse(val, date_format); } public static string tostring(localdate date) { return date == null ? null : date.format(date_format); } }
and change fxml file this:
<person alter="1" checked="false" email="tester1.test@test.com" firstname="tester1" lastname="test"> <date> <util fx:value="2016.01.01"/> </date> </person>
using builderfactory
leave fxml , use custom builderfactory
uses builder
create person
s. builder has getters/setters every property of person
delegate appropriate methods of product except properties require special handling. properties additionally conversion to/from string
.
public class personbuilder implements builder<person> { private final person product = new person(); @override public person build() { return product; } public void setalter(int value) { product.setalter(value); } public void setdate(string value) { product.setdate(util.valueof(value)); } public void setemail(string value) { product.setemail(value); } ... public int getalter() { return product.getalter(); } public string getdate() { return util.tostring(product.getdate()); } public string getemail() { return product.getemail(); } ... }
fxmlloader loader = new fxmlloader(getclass().getresource("person.fxml")); loader.setbuilderfactory(new builderfactory() { private final builderfactory fallback = new javafxbuilderfactory(); @override public builder<?> getbuilder(class<?> type) { return type == person.class ? new personbuilder() : fallback.getbuilder(type); } }); loader.load();
Comments
Post a Comment