java - Have to make Address Book with javaFX Search And Delete Button -
i trying make address book containing person info add
, edit
, search
& delete
options. see below.
- search button: if user add info in blocks instead of pressing
add
if presssearch
info display accordingly - delete button: if user selects row i.e 1 person's details after pressing delete button show deleted data.
- am trying values combobox after add combobox in add button not storing combobox value in tablecell.
my problem when add or delete persondata table delete updating on after press search button why happens.
my current code till yet please help.
import java.util.iterator; import javafx.application.application; import javafx.beans.property.integerproperty; import javafx.beans.property.simpleintegerproperty; import javafx.beans.property.simplestringproperty; import javafx.beans.value.changelistener; import javafx.beans.value.observablevalue; import javafx.collections.fxcollections; import javafx.collections.observablelist; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.geometry.insets; import javafx.scene.group; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.control.combobox; import javafx.scene.control.label; import javafx.scene.control.listcell; import javafx.scene.control.listview; import javafx.scene.control.tablecolumn; import javafx.scene.control.tablecolumn.celleditevent; import javafx.scene.control.tableview; import javafx.scene.control.textfield; import javafx.scene.control.cell.propertyvaluefactory; import javafx.scene.control.cell.textfieldtablecell; import javafx.scene.layout.hbox; import javafx.scene.layout.vbox; import javafx.scene.paint.color; import javafx.scene.text.font; import javafx.stage.stage; import javafx.util.callback; public class myaddressbook extends application { // delete selection private integerproperty index = new simpleintegerproperty(); private tableview<person> table = new tableview<person>(); // filter private observablelist<person> filteredlist = fxcollections.observablearraylist(); private final observablelist<person> data = fxcollections.observablearraylist (new person("jacob", "smith", "male","12 houseno 35street newyork", " 1234341", "12-01-1867","jacob.smith@example.com"), new person("isabella","johnson", "female", "6 houseno 10street california"," 9876521", "12-01-1885", "isabella.johnson@example.com"), new person("ethan", "williams", "male","3 villa house 3street losangels", " 1122331","01-10-1871", "ethan.williams@example.com")); final hbox hb = new hbox(); public static void main(string[] args) { launch(args); } public final double getindex() { return index.get(); } public final void setindex(integer value) { index.set(value); } public integerproperty indexproperty() { return index; } public void filterperson(person filteringperson) { // first clear data in table. filteredlist.clear(); iterator<person> completedata = data.iterator(); while (completedata.hasnext()) { person person = (person) completedata.next(); // if first name matches searching person name, add if (person.getfirstname().contains(filteringperson.getfirstname())) { this.filteredlist.add(person); } } } @override public void start(stage stage) { scene scene = new scene(new group()); stage.settitle("address book"); stage.setwidth(900); stage.setheight(550); final label label = new label("address book"); label.setfont(new font("arial", 20)); combobox gendercombobox = new combobox(); gendercombobox.getitems().addall("male", "female"); gendercombobox.setvalue("male"); gendercombobox .setcellfactory(new callback<listview<string>, listcell<string>>() { @override public listcell<string> call(listview<string> param) { final listcell<string> cell = new listcell<string>() { { super.setprefwidth(100); } @override public void updateitem(string item, boolean empty) { super.updateitem(item, empty); if (item != null) { settext(item); if (item.contains("male")) { settextfill(color.red); } else if (item.contains("female")) { settextfill(color.green); } else { settextfill(color.black); } } else { settext(null); } } }; return cell; } }); table.seteditable(true); tablecolumn<person, string> firstnamecol = new tablecolumn<person, string>( "first name"); firstnamecol.setminwidth(100); firstnamecol .setcellvaluefactory(new propertyvaluefactory<person, string>("firstname")); // firstnamecol.setcellfactory(textfieldtablecell.fortablecolumn()); firstnamecol .setoneditcommit(new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { ((person) t.gettableview().getitems() .get(t.gettableposition().getrow())) .setfirstname(t.getnewvalue()); } }); tablecolumn lastnamecol = new tablecolumn("last name"); lastnamecol.setminwidth(100); lastnamecol .setcellvaluefactory(new propertyvaluefactory<person, string>("lastname")); lastnamecol.setcellfactory(textfieldtablecell.fortablecolumn()); lastnamecol .setoneditcommit(new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { ((person) t.gettableview().getitems() .get(t.gettableposition().getrow())) .setlastname(t.getnewvalue()); } }); tablecolumn gendercol = new tablecolumn("gender"); gendercol.setminwidth(10); gendercol.setcellvaluefactory(new propertyvaluefactory<person, string>("gender")); gendercol.setcellfactory(textfieldtablecell.fortablecolumn()); gendercol .setoneditcommit(new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { ((person) t.gettableview().getitems() .get(t.gettableposition().getrow())) .setgender(t.getnewvalue()); } }); tablecolumn addresscol = new tablecolumn("address"); addresscol.setminwidth(200); addresscol .setcellvaluefactory(new propertyvaluefactory<person, string>("address")); addresscol.setcellfactory(textfieldtablecell.fortablecolumn()); addresscol .setoneditcommit(new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { ((person) t.gettableview().getitems() .get(t.gettableposition().getrow())) .setaddress(t.getnewvalue()); } }); tablecolumn phonenocol = new tablecolumn("phone no"); phonenocol.setminwidth(100); phonenocol .setcellvaluefactory(new propertyvaluefactory<person, string>("phoneno")); phonenocol.setcellfactory(textfieldtablecell.fortablecolumn()); phonenocol .setoneditcommit(new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { ((person) t.gettableview().getitems() .get(t.gettableposition().getrow())) .setphoneno(t.getnewvalue()); } }); tablecolumn dateofbirthcol = new tablecolumn("date of birth"); dateofbirthcol.setminwidth(100); dateofbirthcol .setcellvaluefactory(new propertyvaluefactory<person, string>("dateofbirth")); dateofbirthcol.setcellfactory(textfieldtablecell.fortablecolumn()); dateofbirthcol .setoneditcommit(new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { ((person) t.gettableview().getitems() .get(t.gettableposition().getrow())) .setdateofbirth(t.getnewvalue()); } }); tablecolumn emailcol = new tablecolumn("email"); emailcol.setminwidth(150); emailcol.setcellvaluefactory(new propertyvaluefactory<person, string>("email")); emailcol.setcellfactory(textfieldtablecell.fortablecolumn()); emailcol.setoneditcommit(new eventhandler<celleditevent<person, string>>() { @override public void handle(celleditevent<person, string> t) { ((person) t.gettableview().getitems() .get(t.gettableposition().getrow())).setemail(t .getnewvalue()); } }); table.setitems(filteredlist); filteredlist.addall(data); table.getcolumns().addall(firstnamecol, lastnamecol, gendercol, addresscol, phonenocol, dateofbirthcol, emailcol); final textfield addfirstname = new textfield(); addfirstname.setprompttext("first name"); addfirstname.setmaxwidth(firstnamecol.getprefwidth()); final textfield addlastname = new textfield(); addlastname.setmaxwidth(lastnamecol.getprefwidth()); addlastname.setprompttext("last name"); final textfield addgender = new textfield(); addgender.setmaxwidth(gendercol.getprefwidth()); addgender.setprompttext("gender"); final textfield addaddress = new textfield(); addaddress.setmaxwidth(addresscol.getprefwidth()); addaddress.setprompttext("address"); final textfield addphoneno = new textfield(); addphoneno.setmaxwidth(phonenocol.getprefwidth()); addphoneno.setprompttext("phoneno"); final textfield adddateofbirth = new textfield(); adddateofbirth.setmaxwidth(dateofbirthcol.getprefwidth()); adddateofbirth.setprompttext("dateofbirth"); final textfield addemail = new textfield(); addemail.setmaxwidth(emailcol.getprefwidth()); addemail.setprompttext("email"); final button addbutton = new button("add"); addbutton.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { data.add(new person(addfirstname.gettext(), addlastname.gettext(), addaddress.gettext(), addphoneno.gettext(), adddateofbirth.gettext(), addemail.gettext(), addgender.gettext())); addfirstname.clear(); addlastname.clear(); addaddress.clear(); addphoneno.clear(); adddateofbirth.clear(); addemail.clear(); addgender.clear(); } }); table.getselectionmodel().selecteditemproperty() .addlistener(new changelistener() { @override public void changed(observablevalue observable, object oldvalue, object newvalue) { person selectedperson = (person) newvalue; setindex(data.indexof(newvalue)); system.out.println("ok"); } }); final button delbutton = new button("delete"); delbutton.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { data.remove(index.get()); table.getselectionmodel().clearselection(); } }); final button searchbutton = new button("search"); searchbutton.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { person person = new person(addfirstname.gettext(), addlastname.gettext(), addaddress.gettext(), addphoneno.gettext(), adddateofbirth.gettext(), addemail.gettext(), addgender.gettext()); addfirstname.clear(); addlastname.clear(); addaddress.clear(); addphoneno.clear(); adddateofbirth.clear(); addemail.clear(); addgender.clear(); filterperson(person); } }); hb.getchildren().addall(addfirstname, addlastname, gendercombobox,addaddress, addphoneno, adddateofbirth, addemail, addbutton, delbutton, searchbutton); hb.setspacing(3); final vbox vbox = new vbox(); vbox.setspacing(5); vbox.setpadding(new insets(10, 0, 0, 10)); vbox.getchildren().addall(label, table, hb); ((group) scene.getroot()).getchildren().addall(vbox); stage.setscene(scene); stage.show(); } public static class person implements comparable<person> { private final simplestringproperty firstname; private final simplestringproperty lastname; private final simplestringproperty gender; private final simplestringproperty address; private final simplestringproperty phoneno; private final simplestringproperty dateofbirth; private final simplestringproperty email; private person(string fname, string lname, string gen, string address, string phno, string dob, string email) { this.firstname = new simplestringproperty(fname); this.lastname = new simplestringproperty(lname); this.gender = new simplestringproperty(gen); this.address = new simplestringproperty(address); this.phoneno = new simplestringproperty(phno); this.dateofbirth = new simplestringproperty(dob); this.email = new simplestringproperty(email); } public string getfirstname() { return firstname.get(); } public void setfirstname(string fname) { firstname.set(fname); } public string getlastname() { return lastname.get(); } public void setlastname(string fname) { lastname.set(fname); } public void setgender(string gen) { gender.set(gen); } public string getgender() { return gender.get(); } public string getaddress() { return address.get(); } public void setaddress(string add) { address.set(add); } public string getphoneno() { return phoneno.get(); } public void setphoneno(string phno) { phoneno.set(phno); } public string getdateofbirth() { return dateofbirth.get(); } public void setdateofbirth(string dob) { dateofbirth.set(dob); } public string getemail() { return email.get(); } public void setemail(string fname) { email.set(fname); } @override public int compareto(person o) { return firstname.get().comparetoignorecase(o.getfirstname()); } } }
this creates button
final button addbutton = new button("add"); addbutton.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { data.add(new person( addfirstname.gettext(), addlastname.gettext(), addaddress.gettext(), addphoneno.gettext(), adddateofbirth.gettext(), addemail.gettext(), addgender.gettext())); addfirstname.clear(); addlastname.clear(); addaddress.clear(); addphoneno.clear(); adddateofbirth.clear(); addemail.clear(); addgender.clear(); } });
and line added elements scene
hb.getchildren().addall(addfirstname, addlastname, addgender, addaddress, addphoneno, adddateofbirth, addemail, addbutton);
so create buttons
final button deletebutton = new button("delete"); addbutton.setonaction(new eventhandler<actionevent>() { @override public void handle(actionevent e) { // delete date here } });
and add list like
hb.getchildren().addall(addfirstname, addlastname, addgender, addaddress, addphoneno, adddateofbirth, addemail, addbutton, deletebutton, searchbutton);
Comments
Post a Comment