Listen to if a button is disabled or enabled in JavaFX -
i want code allows me listen changes made on textfield
component disabling or enabling button called save
. possible using bindings or similar?
for example: save.disableproperty().bind(...)
used 2 textfield
components changing information stored in tableview
.
used data binding when tableview
row selected, information in row displayed in textfield
.
@fxml private tableview<userdata> table; @fxml private tablecolumn<userdata, string> col1; @fxml private tablecolumn<userdata, string> col2; @fxml private textfield name; @fxml private textfield email; @fxml button newuser; @fxml button save; @fxml button delete; private connection conn; private statement stm; private preparedstatement prep; private resultset result; /** * initializes controller class. */ @override public void initialize(url url, resourcebundle rb) { populatetableview(); configuredisable(); table.getselectionmodel().selecteditemproperty().addlistener(userdatalistener); name.disableproperty().bind(table.getselectionmodel().selecteditemproperty().isnull()); email.disableproperty().bind(table.getselectionmodel().selecteditemproperty().isnull()); delete.disableproperty().bind(table.getselectionmodel().selecteditemproperty().isnull()); data.addlistener((change<? extends userdata> change) -> save.setdisable(false)); name.setonaction(ev -> email.requestfocus()); } private final changelistener<userdata> userdatalistener = ((v, oldv, newv) -> { if (oldv != null) { name.textproperty().unbindbidirectional(oldv.nameproperty()); email.textproperty().unbindbidirectional(oldv.emailproperty()); } if (newv != null) { name.textproperty().bindbidirectional(newv.nameproperty()); email.textproperty().bindbidirectional(newv.emailproperty()); } private void populatetableview() { try { conn = createconnection.getconnection(); stm = conn.createstatement(); data = fxcollections.observablearraylist(); result = conn.createstatement().executequery("select *from user"); while (result.next()) { data.add(new userdata(result.getstring("name"), result.getstring("email"))); } col1.setcellvaluefactory(new propertyvaluefactory<userdata, string>("name")); col2.setcellvaluefactory(new propertyvaluefactory<userdata, string>("email")); table.setitems(data); } catch (ioexception ex) { logger.getlogger(fxmlcontroller.class.getname()).log(level.severe, null, ex); } catch (sqlexception ex) { logger.getlogger(fxmlcontroller.class.getname()).log(level.severe, null, ex); } } });
if understand correctly, using text fields change data in table (specifically here name
, email
properties), , when values in table changed want enable save button.
you need listeners (rather bindings) this. 1 way create observablelist
table items has extractor extract properties of interest. when list changes (including properties list elements), set disable property of save button false.
something like:
observablelist<userdata> observabledata = fxcollections.observablelist(table.getitems(), user -> new observable[] {user.nameproperty(), user.emailproperty()}); observabledata.addlistener((change<? extends person> change) -> save.setdisable(false));
Comments
Post a Comment