java - Arraylist is not returning the object -
i dunno missing concept, have coded small project maintains contacts using java arraylist , gui. there add, delete,seach,first,last,previous,next,exit,clear buttons in application. buttons except first , last working fine. problem getting in function displayfirst**()
here code
addressbookdemo.java
public void actionperformed (actionevent e){ if (e.getsource () == jbbsave){ saveperson(); clear(); } else if (e.getsource() == jbndelete){ deleteperson(); clear(); } else if (e.getsource() == jbnfirst){ displayfirstrecord(); clear(); } else if (e.getsource() == jbnlast){ displaylastrecord(); clear(); } else if (e.getsource() == jbnsearch){ searchperson(); } else if (e.getsource() == jbnforward){ displaynextrecord(); } else if (e.getsource() == jbnback){ displaypreviousrecord(); } else if (e.getsource() == jbnclear){ clear(); } else if (e.getsource() == jbnexit){ system.exit(0); } } public void searchperson() { name = jtfname.gettext(); name = name.touppercase(); /*clear contents of arraylist if there previous search*/ personslist.clear(); if(name.equals("")){ joptionpane.showmessagedialog(null,"please enter person name search."); } else{ /*get array list of searched persons using persondao*/ personslist = pdao.searchperson(name); if(personslist.size() == 0){ joptionpane.showmessagedialog(null, "no records found."); //perform clear if no records found. clear(); } else { /*downcast object array list personinfo*/ personinfo person = (personinfo) personslist.get(0); // displaying search record in text fields jtfname.settext(person.getname()); jtfaddress.settext(person.getaddress()); jtfphone.settext(""+person.getphone()); jtfemail.settext(person.getemail()); } } } **** public void displayfirstrecord(){ // dec in recordnumber display previous person info, //stored in personslist during search if(pdao.getpersonslist().size() == 0 ){ joptionpane.showmessagedialog(null, "there no records"); /*if user has reached begining of results, disable button*/ jbnforward.setenabled(true); jbnback.setenabled(false); // inc 1 counter last dec }else{ system.out.println(pdao.getpersonslist().size()); personslist = pdao.getpersonslist(); personinfo person=(personinfo)personslist.get(0); // displaying search record in text fields jtfname.settext(person.getname()); jtfaddress.settext(person.getaddress()); jtfphone.settext(""+person.getphone()); jtfemail.settext(person.getemail()); //jbnfirst.setenabled(false); } } public void displaylastrecord(){ // dec in recordnumber display previous person info, //stored in personslist during search if(pdao.getpersonslist().size()== 0 ){ joptionpane.showmessagedialog(null, "there no records"); /*if user has reached begining of results, disable button*/ jbnforward.setenabled(true); jbnback.setenabled(false); // inc 1 counter last dec }else{ jbnforward.setenabled(true); personinfo person = (personinfo) pdao.getpersonslist().get(pdao.getpersonslist().size()-1); // displaying search record in text fields jtfname.settext(person.getname()); jtfaddress.settext(person.getaddress()); jtfphone.settext(""+person.getphone()); jtfemail.settext(person.getemail()); } } ********************
persondao.java
public class persondao{ private arraylist<personinfo> personslist; ******************************* public arraylist searchperson(string name) { arraylist<personinfo> personslist1=new arraylist<>(); (personinfo personslist2 : personslist) { personinfo p = (personinfo) personslist2; if(p.getname() == null ? name == null : p.getname().equals(name)){ personslist1.add(p); break; } } return personslist1; }
personinfo.java
contains person class.
you're calling clear()
method clears text of jlable
s
else if (e.getsource() == jbnfirst){ displayfirstrecord(); clear(); } else if (e.getsource() == jbnlast){ displaylastrecord(); clear(); }
....
public void displayfirstrecord(){ .... jtfname.settext(person.getname()); jtfaddress.settext(person.getaddress()); jtfphone.settext(""+person.getphone()); jtfemail.settext(person.getemail()); .... }
.... displaylastrecord()
method same thing.
public void clear(){ jtfname.settext(""); jtfaddress.settext(""); jtfphone.settext(""); jtfemail.settext(""); .... }
Comments
Post a Comment