updated code show classes
person
class has setters , getters because have user input.(hard coding now) here code creating address book. have 4 panels far want switch between depending on buttons press. using gridbaglayout
along borderlayout
, decided cardlayout
proper way go? tried implementing cardlayout
getting error.
exception in thread "main": java.lang.nullpointerexception @ jtablesortingperson.<init>(jtablesortingperson.java:30) @ addressbook.main(addressbook.java:100)
line 30 me setting cardlayout
mainpanel
.
line 100 me creating class object in main set layout visible, etc...
i having trouble sizing each panel fit or auto resize jframe
. beginner/student of computer science. appreciated. also, first time using site, sorry.
class addressbook
:
import java.util.*; import javax.swing.jframe; public class addressbook{ public static void main(string[] args) { jtablesortingperson p = new jtablesortingperson(); p.setdefaultcloseoperation(jframe.dispose_on_close); p.setsize(800, 450); p.setvisible(true); } }
class person
:
public class person { string firstname; string middlename; string lastname; string streetaddress; int aptnumber; string city; string state; int zipcode; string phonenumber; //change long after triallist string email; string group; string specialdate; string comments; public person(string fn, string mn, string ln, string st, int apt, string city, string state, int zip, string phone, string email, string group, string date, string comments){ firstname = fn; middlename = mn; lastname = ln; streetaddress = st; aptnumber = apt; this.city = city; this.state = state; zipcode = zip; phonenumber = phone; this.email = email; this.group = group; specialdate = date; this.comments = comments; } public string tostring(){ return "first name: " + firstname + " \nmiddle name: " + middlename + " \nlast name: " + lastname + " \nstreet address: " + streetaddress + " \napt: " + aptnumber + " \ncity: " + city + " \nstate: " + state + " \nzip: " + zipcode + " \nphone: " + phonenumber + " \nemail: " + email + " \ngroup: " + group + " \ndates: " + specialdate + " \ncomments " + "***" + comments + "***\n\n"; } public string getfirstname() { return firstname; } public void setfirstname(string firstname) { this.firstname = firstname; } public string getmiddlename() { return middlename; } public void setmiddlename(string middlename) { this.middlename = middlename; } public string getlastname() { return lastname; } public void setlastname(string lastname) { this.lastname = lastname; } public string getstreetaddress() { return streetaddress; } public void setstreetaddress(string streetaddress) { this.streetaddress = streetaddress; } public int getaptnumber() { return aptnumber; } public void setaptnumber(int aptnumber) { this.aptnumber = aptnumber; } public string getcity() { return city; } public void setcity(string city) { this.city = city; } public string getstate() { return state; } public void setstate(string state) { this.state = state; } public int getzipcode() { return zipcode; } public void setzipcode(int zipcode) { this.zipcode = zipcode; } public string getphonenumber() { return phonenumber; } public void setphonenumber(string phonenumber) { this.phonenumber = phonenumber; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getgroup() { return group; } public void setgroup(string group) { this.group = group; } public string getspecialdate() { return specialdate; } public void setspecialdate(string specialdate) { this.specialdate = specialdate; } public string getcomments() { return comments; } public void setcomments(string comments) { this.comments = comments; } }
class persontable
:
import javax.swing.table.abstracttablemodel; import java.util.*; public class persontable extends abstracttablemodel { private static final int column_first_name = 0; private static final int column_last_name = 1; private static final int column_phone_number = 2; private static final int column_city = 3; private string[] columnnames = {"first name", "last name", "phone number", "city"}; private list<person> listperson; public persontable(list<person> listperson){ this.listperson = listperson; } @override public int getcolumncount() { return columnnames.length; } @override public int getrowcount() { return listperson.size(); } @override public object getvalueat(int rowindex, int columnindex) { person person = listperson.get(rowindex); object retval = null; switch(columnindex){ case column_first_name: retval = person.getfirstname(); break; case column_last_name: retval = person.getlastname(); break; case column_phone_number: retval = person.getphonenumber(); break; case column_city: retval = person.getcity(); break; default: throw new illegalargumentexception("invalid column index."); } return retval; } @override public string getcolumnname(int columnindex){ return columnnames[columnindex]; } @override public class<?> getcolumnclass(int columnindex){ if(listperson.isempty()) return object.class; else return getvalueat(0, columnindex).getclass(); } }
class persontable2
:
import javax.swing.table.abstracttablemodel; import java.util.*; public class persontable2 extends abstracttablemodel{ private static final int column_first_name = 0; private static final int column_middle_name = 1; private static final int column_last_name = 2; private static final int column_phone_number = 3; private static final int column_city = 4; private static final int column_state = 5; private static final int column_email = 6; private static final int column_group = 7; private string[] columnnames = {"first name", "middle name", "last name", "phone number", "city", "state", "email", "group"}; private list<person> listperson; public persontable2(list<person> listperson){ this.listperson = listperson; } @override public int getcolumncount() { return columnnames.length; } @override public int getrowcount() { return listperson.size(); } @override public object getvalueat(int rowindex, int columnindex) { person person = listperson.get(rowindex); object retval = null; switch(columnindex){ case column_first_name: retval = person.getfirstname(); break; case column_middle_name: retval = person.getmiddlename(); break; case column_last_name: retval = person.getlastname(); break; case column_phone_number: retval = person.getphonenumber(); break; case column_city: retval = person.getcity(); break; case column_state: retval = person.getstate(); break; case column_email: retval = person.getemail(); break; case column_group: retval = person.getgroup(); break; default: throw new illegalargumentexception("invalid column index."); } return retval; } @override public string getcolumnname(int columnindex){ return columnnames[columnindex]; } @override public class<?> getcolumnclass(int columnindex){ if(listperson.isempty()) return object.class; else return getvalueat(0, columnindex).getclass(); } }
class jtablesortingperson
:
import java.awt.*; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.io.file; import java.io.filenotfoundexception; import java.util.list; //important import java.util.*; import javax.swing.*; import javax.swing.table.tablemodel; import javax.swing.table.tablerowsorter; @suppresswarnings("serial") public class jtablesortingperson extends jframe{ private jpanel mainpanel, panel, panel2, intropanel, newuserpanel, editpanel; private jtextarea textarea; private jtextarea textarea2; private jtextfield searchinput, username, password, newuser, newpass, confirmpassword; private jtextfield searchinput2; private jlabel statusbar, user, pass, newusername, newpassword, newconfirmpassword; private jlabel statusbar2; private jtable table, table2; private jbutton detailbutton, searchbutton, allbutton, loginbutton, newbutton, savebutton, cancelbutton, addbutton, removebutton, editbutton; private jbutton detailbutton2, searchbutton2, goback, loginbutton2, newbutton2, savebutton2, cancelbutton2, addbutton2, removebutton2, editbutton2; private cardlayout cardlayout = new cardlayout(); list<person> listperson = createlistperson(); public jtablesortingperson() { super(); mainpanel.setlayout(cardlayout); //*********************************************** //log in panel //*********************************************** intropanel = new jpanel(); gridbaglayout gridlayout = new gridbaglayout(); intropanel.setlayout(gridlayout); gridbagconstraints c = new gridbagconstraints(); //add(intropanel, borderlayout.center); mainpanel.add(intropanel, "log in"); newbutton = new jbutton("new user"); newbutton.addactionlistener(new buttonlistener()); loginbutton = new jbutton("log in"); loginbutton.addactionlistener(new buttonlistener()); username = new jtextfield(); password = new jtextfield(); user = new jlabel("username:"); pass = new jlabel("password:"); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 1; intropanel.add(user, c); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 3; intropanel.add(pass, c); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 2; c.gridwidth = 1000; intropanel.add(username, c); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 4; c.gridwidth = 1000; intropanel.add(password, c); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 5; c.gridwidth = 1000; intropanel.add(loginbutton, c); c.fill = gridbagconstraints.horizontal; c.gridx = 0; c.gridy = 6; c.gridwidth = 1000; intropanel.add(newbutton, c); pack(); setdefaultcloseoperation(jframe.exit_on_close); setlocationrelativeto(null); //*********************************************** //main table panel //*********************************************** panel = new jpanel(); gridbaglayout gridlayout2 = new gridbaglayout(); panel.setlayout(gridlayout2); gridbagconstraints c2 = new gridbagconstraints(); persontable tablemodel = new persontable(listperson); table = new jtable(tablemodel); table.setautocreaterowsorter(true); detailbutton = new jbutton("details"); detailbutton.addactionlistener(new buttonlistener()); searchbutton = new jbutton("search"); searchbutton.addactionlistener(new buttonlistener()); allbutton = new jbutton("display all"); allbutton.addactionlistener(new buttonlistener()); addbutton = new jbutton("add person"); addbutton.addactionlistener(new buttonlistener()); removebutton = new jbutton("remove person"); removebutton.addactionlistener(new buttonlistener()); searchinput = new jtextfield(); textarea = new jtextarea(); statusbar = new jlabel(); //add(panel, borderlayout.center); mainpanel.add(panel, "edit person table"); add(statusbar, borderlayout.south); panel.add(new jscrollpane(table), c2); c2.fill = gridbagconstraints.horizontal; c2.gridx = 0; c2.gridy = 1; c2.gridwidth = 1000; panel.add(searchinput, c2); c2.fill = gridbagconstraints.horizontal; c2.gridx = 0; c2.gridy = 2; c2.gridwidth = 1000; panel.add(searchbutton, c2); c2.fill = gridbagconstraints.horizontal; c2.gridx = 0; c2.gridy = 3; c2.gridwidth = 1000; panel.add(detailbutton, c2); c2.fill = gridbagconstraints.horizontal; c2.gridx = 0; c2.gridy = 4; c2.gridwidth = 1000; panel.add(allbutton, c2); c2.fill = gridbagconstraints.horizontal; c2.gridx = 0; c2.gridy = 5; c2.gridwidth = 1000; panel.add(addbutton, c2); c2.fill = gridbagconstraints.horizontal; c2.gridx = 0; c2.gridy = 6; c2.gridwidth = 1000; panel.add(removebutton, c2); pack(); setdefaultcloseoperation(jframe.exit_on_close); setlocationrelativeto(null); panel.setvisible(false); //*********************************************** //create new user panel //*********************************************** newuserpanel = new jpanel(); gridbaglayout gridbaglayout3 = new gridbaglayout(); newuserpanel.setlayout(gridbaglayout3); gridbagconstraints c3 = new gridbagconstraints(); //add(newuserpanel, borderlayout.center); mainpanel.add(newuserpanel, "new user"); newuser = new jtextfield(); newpass = new jtextfield(); confirmpassword = new jtextfield(); newusername = new jlabel("new username:"); newpassword = new jlabel("new password:"); newconfirmpassword = new jlabel("confirm password"); savebutton = new jbutton("save"); savebutton.addactionlistener(new buttonlistener()); cancelbutton = new jbutton("cancel"); cancelbutton.addactionlistener(new buttonlistener()); c3.fill = gridbagconstraints.horizontal; c3.gridx = 0; c3.gridy = 1; c3.gridwidth = 1000; newuserpanel.add(newusername, c3); c3.fill = gridbagconstraints.horizontal; c3.gridx = 0; c3.gridy = 2; c3.gridwidth = 1000; newuserpanel.add(newuser, c3); c3.fill = gridbagconstraints.horizontal; c3.gridx = 0; c3.gridy = 3; c3.gridwidth = 1000; newuserpanel.add(newpassword, c3); c3.fill = gridbagconstraints.horizontal; c3.gridx = 0; c3.gridy = 4; c3.gridwidth = 1000; newuserpanel.add(newpass, c3); c3.fill = gridbagconstraints.horizontal; c3.gridx = 0; c3.gridy = 5; c3.gridwidth = 1000; newuserpanel.add(newconfirmpassword, c3); c3.fill = gridbagconstraints.horizontal; c3.gridx = 0; c3.gridy = 6; c3.gridwidth = 1000; newuserpanel.add(confirmpassword, c3); c3.fill = gridbagconstraints.horizontal; c3.gridx = 0; c3.gridy = 7; c3.gridwidth = 1000; newuserpanel.add(savebutton, c3); c3.fill = gridbagconstraints.horizontal; c3.gridx = 0; c3.gridy = 8; c3.gridwidth = 1000; newuserpanel.add(cancelbutton, c3); pack(); setdefaultcloseoperation(jframe.exit_on_close); setlocationrelativeto(null); newuserpanel.setvisible(false); //*********************************************** //main table panel2 //*********************************************** panel2 = new jpanel(); gridbaglayout gridlayout4 = new gridbaglayout(); panel2.setlayout(gridlayout4); gridbagconstraints c4 = new gridbagconstraints(); persontable2 tablemodel2 = new persontable2(listperson); table2 = new jtable(tablemodel2); table2.setautocreaterowsorter(true); detailbutton2 = new jbutton("details"); detailbutton2.addactionlistener(new buttonlistener()); searchbutton2 = new jbutton("search"); searchbutton2.addactionlistener(new buttonlistener()); goback = new jbutton("back"); goback.addactionlistener(new buttonlistener()); searchinput2 = new jtextfield(); textarea2 = new jtextarea(); statusbar2 = new jlabel(); //add(panel2, borderlayout.center); mainpanel.add(panel2, "all person table"); add(statusbar2, borderlayout.south); panel2.add(new jscrollpane(table2), c4); c4.fill = gridbagconstraints.horizontal; c4.gridx = 0; c4.gridy = 1; c4.gridwidth = 1000; panel2.add(searchinput2, c4); c4.fill = gridbagconstraints.horizontal; c4.gridx = 0; c4.gridy = 2; c4.gridwidth = 1000; panel2.add(searchbutton2, c4); c4.fill = gridbagconstraints.horizontal; c4.gridx = 0; c4.gridy = 3; c4.gridwidth = 1000; panel2.add(detailbutton2, c4); c4.fill = gridbagconstraints.horizontal; c4.gridx = 0; c4.gridy = 4; c4.gridwidth = 1000; panel2.add(goback, c4); pack(); setdefaultcloseoperation(jframe.exit_on_close); setlocationrelativeto(null); panel2.setvisible(false); //*********card layout********* //mainpanel.add(intropanel, "log in"); //mainpanel.add(newuserpanel, "new user"); //mainpanel.add(panel, "edit person table"); //mainpanel.add(panel2, "all person table"); this.setcontentpane(mainpanel); cardlayout.show(mainpanel, "intropanel"); } public list<person> createlistperson(){ arraylist<person> retval = new arraylist<person>(); retval.add(new person("nicolas", "lawrence", "soto", "10513 demilo pl", 110, "orlando", "fl", 32836, "3052159446", "nsoto0216@hotmail.com", "self", "02/16/1991", "i student.")); retval.add(new person("sebastian", "matias", "soto", "10513 demilo pl", 110, "orlando", "fl", 32836, "3213559306", "ssoto1005@hotmail.com", "family", "10/05/1992", "my little brother.")); retval.add(new person("montserrat", "soto", "casco", "12401 nw 185 st", 0, "miami", "fl", 33161, "3052023390", "msoto0331@gmail.com", "family", "03/31/1987", "my sister.")); retval.add(new person("marcelo", "suarez", "soto", "4420 nw 16 st", 203, "miami", "fl", 33055, "3213559355", "chefchileno3@hotmail.com", "family", "10/28/1955", "my father.")); retval.add(new person("olga", "beatriz", "munizaga", "4420 nw 16 st", 203, "miami", "fl", 33055, "3213559356", "omunizaga0326@hotmail.com", "family", "03/26/1959", "my mother.")); retval.add(new person("linda", "cecilia", "chavez", "3256 nw 12 st", 0, "miami", "fl", 33056, "3056234456", "lindizee09@hotmail.com", "friend", "10/10/1991", "ex girlfriend.")); retval.add(new person("laura", "perez", "carrasco", "500 pinnacle cove", 203, "orlando", "fl", 32078, "4072180248", "lcarrasco0325@hotmail.com", "friend", "03/25/1991", "ex girlfriend.")); retval.add(new person("jenn", "", "ortiz", "2034 lanstar blvd", 0, "orlando", "fl", 32724, "8084735466", "jenn456@gmail.com", "friend", "07/17/1993", "crazy girl")); retval.add(new person("cristian", "", "camacho", "4556 americana blvd", 308, "orlando", "fl", 32856, "4076557890", "chrisc176@hotmail.com", "friend", "12/02/1989", "")); retval.add(new person("gabriela", "", "valdez", "3004 watercrest ln", 109, "orlando", "fl", 32747, "4073066789", "gabbyvaldez@gmail.com", "friend", "07/15/1992", "moved california")); retval.add(new person("devin", "", "delaney", "", 0, "orlando", "fl", 32836, "4074259556", "ddevin64@hotmail.com", "coworker", "02/23/1993", "my favorite coworker")); return retval; } public class buttonlistener implements actionlistener { @override public void actionperformed(actionevent e) { if (e.getsource() == detailbutton) { newuserpanel.invalidate(); newuserpanel.setvisible(false); intropanel.invalidate(); intropanel.setvisible(false); panel2.invalidate(); panel2.setvisible(false); panel.validate(); panel.setvisible(true); int row = table.getselectedrow(); if (row == -1) { statusbar.settext("make selection"); joptionpane.showmessagedialog(null, "no selection made"); } else { statusbar.settext("showing details..."); row = table.convertrowindextomodel(table.getselectedrow()); joptionpane.showmessagedialog(null, listperson.get(row)); } } else if (e.getsource() == searchbutton) { newuserpanel.invalidate(); newuserpanel.setvisible(false); intropanel.invalidate(); intropanel.setvisible(false); panel2.invalidate(); panel2.setvisible(false); panel.validate(); panel.setvisible(true); jtextfieldactionperformed(e); } else if (e.getsource() == allbutton) { statusbar.settext("showing people..."); /*jtextarea textarea = new jtextarea(listperson.tostring()); jscrollpane scroll = new jscrollpane(textarea); jdialog displayall = new jdialog(); displayall.setvisible(true); displayall.setsize(350, 750); displayall.add(scroll);*/ statusbar.settext("done"); intropanel.invalidate(); intropanel.setvisible(false); newuserpanel.invalidate(); newuserpanel.setvisible(false); panel.invalidate(); panel.setvisible(false); panel2.validate(); panel2.setvisible(true); } else if (e.getsource() == addbutton){ } else if (e.getsource() == removebutton){ newuserpanel.invalidate(); newuserpanel.setvisible(false); intropanel.invalidate(); intropanel.setvisible(false); panel2.invalidate(); panel2.setvisible(false); panel.validate(); panel.setvisible(true); int row = table.getselectedrow(); if (row == -1) { joptionpane.showmessagedialog(null, "no selection made"); } else { row = table.convertrowindextomodel(table.getselectedrow()); listperson.remove(row); table.addnotify(); //joptionpane.showmessagedialog(null, listperson.get(row)); } } else if (e.getsource() == detailbutton2) { panel.invalidate(); panel.setvisible(false); newuserpanel.invalidate(); newuserpanel.setvisible(false); intropanel.invalidate(); intropanel.setvisible(false); panel2.validate(); panel2.setvisible(true); int row = table2.getselectedrow(); if (row == -1) { statusbar2.settext("make selection"); joptionpane.showmessagedialog(null, "no selection made"); } else { statusbar2.settext("showing details..."); row = table2.convertrowindextomodel(table2.getselectedrow()); joptionpane.showmessagedialog(null, listperson.get(row)); } } else if (e.getsource() == searchbutton2) { panel.invalidate(); panel.setvisible(false); newuserpanel.invalidate(); newuserpanel.setvisible(false); intropanel.invalidate(); intropanel.setvisible(false); panel2.validate(); panel2.setvisible(true); jtextfieldactionperformed2(e); } else if (e.getsource() == goback) { /*jtextarea textarea = new jtextarea(listperson.tostring()); jscrollpane scroll = new jscrollpane(textarea); jdialog displayall = new jdialog(); displayall.setvisible(true); displayall.setsize(350, 750); displayall.add(scroll);*/ statusbar2.settext("done"); intropanel.invalidate(); intropanel.setvisible(false); newuserpanel.invalidate(); newuserpanel.setvisible(false); panel2.invalidate(); panel2.setvisible(false); panel.validate(); panel.setvisible(true); } else if (e.getsource() == loginbutton){ intropanel.invalidate(); intropanel.setvisible(false); newuserpanel.invalidate(); newuserpanel.setvisible(false); panel2.invalidate(); panel2.setvisible(false); panel.validate(); panel.setvisible(true); //pack(); //joptionpane.showmessagedialog(null, "you attempted log in"); } else if (e.getsource() == newbutton){ intropanel.invalidate(); intropanel.setvisible(false); panel.invalidate(); panel.setvisible(false); panel2.invalidate(); panel2.setvisible(false); newuserpanel.validate(); newuserpanel.setvisible(true); //pack(); //joptionpane.showmessagedialog(null, "you attempted create account"); } else if (e.getsource() == savebutton){ } else if (e.getsource() == cancelbutton){ panel.invalidate(); panel.setvisible(false); panel2.invalidate(); panel2.setvisible(false); newuserpanel.invalidate(); newuserpanel.setvisible(false); intropanel.validate(); intropanel.setvisible(true); } } } public void jtextfieldactionperformed(java.awt.event.actionevent evt) { tablerowsorter<tablemodel> sorter = new tablerowsorter<tablemodel>(((persontable) table.getmodel())); sorter.setrowfilter(rowfilter.regexfilter("(?i)" + searchinput.gettext())); //"(?i)" case sensitivity table.setrowsorter(sorter); } public void jtextfieldactionperformed2(java.awt.event.actionevent evt) { tablerowsorter<tablemodel> sorter = new tablerowsorter<tablemodel>(((persontable2) table2.getmodel())); sorter.setrowfilter(rowfilter.regexfilter("(?i)" + searchinput2.gettext())); //"(?i)" case sensitivity table2.setrowsorter(sorter); } public void newuser(){ /*try { } catch (filenotfoundexception e) { e.printstacktrace(); }*/ } public void login(string user, string pass){ } }
i think not guy stop trying you, sorry :p
your code insane read, , need improve java skills. i'm pretty sure bug magically disappear after code cleaning !
so give (and sorry that, not direct solution problem) suggest ideas refactor code:
avoid classes more approximatively 10 attributes; use more classes represent data !
secondly, try use enums
(it's easy, see) instead of sort of things:
private static final int column_first_name = 0; private static final int column_last_name = 1; private static final int column_phone_number = 2; private static final int column_city = 3;
and suggest break jtablesortingperson
many little components instead of having basic bricks @ same place. it's build tower directly sand instead of using bricks made of sand.
i can many , many things improve code, vex first, , make crazy of technical things give !
good luck refactoring, it's key think !
Comments
Post a Comment