java - Having issues with custom linked list and applying to JTable -
i'm having issues creating jtable using custom table model , custom linked list. i'm hoping there easy changes i'm missing.
my main class:
import java.util.arraylist; import java.util.*; import java.io.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import javax.swing.table.*; public class studentinterface { public static void main(string[] args){ new studentinterface(); } //create linked list mylinkedlist studentpack = new mylinkedlist(); public studentinterface (){ //create jframe jframe jframe = new jframe("student information interface"); jframe.setdefaultcloseoperation(jframe.exit_on_close); jframe.setsize(400,300); jframe.setvisible(true); jframe.setdefaultlookandfeeldecorated(true); jframe.setpreferredsize(new dimension(600, 400)); //create content panes jpanel headerpanel = new jpanel(); jpanel tablepanel = new jpanel(); jpanel buttonpanel = new jpanel(); jpanel combinedpanel = new jpanel(); headerpanel.setlayout(new borderlayout()); tablepanel.setlayout(new flowlayout()); buttonpanel.setlayout(new flowlayout()); combinedpanel.setlayout(new borderlayout()); //add label header content panel jlabel headerlabel1 = new jlabel("welcome student information interface"); jlabel headerlabel2 = new jlabel("***click cell edit data***"); jlabel headerlabel3 = new jlabel("***click column header sort***"); headerlabel1.sethorizontalalignment(jlabel.center); headerlabel2.sethorizontalalignment(jlabel.center); headerlabel3.sethorizontalalignment(jlabel.center); headerpanel.add(headerlabel1, borderlayout.north); headerpanel.add(headerlabel2, borderlayout.center); headerpanel.add(headerlabel3, borderlayout.south); //where gui created: jmenubar menubar; jmenu menu, submenu; jmenuitem exitmenuitem; jmenuitem savemenuitem; jmenuitem loadmenuitem; //create menu bar. menubar = new jmenubar(); //build main menu. menu = new jmenu("file"); menu.setmnemonic(keyevent.vk_a); menu.getaccessiblecontext().setaccessibledescription("the menu in program has menu items"); menubar.add(menu); //create menu items exitmenuitem = new jmenuitem("exit",keyevent.vk_t); menu.add(exitmenuitem); jframe.setjmenubar(menubar); //create table tablemodel model; model = new tablemodel(new mylinkedlist()); jtable table = new jtable(model); //add list list content panel jscrollpane scrollpane = new jscrollpane(table); scrollpane.setpreferredsize(new dimension(580, 400)); tablepanel.add(scrollpane); //add buttons button content panel jbutton addbutton = new jbutton("add"); jbutton removebutton = new jbutton("remove"); buttonpanel.add(addbutton); buttonpanel.add(removebutton); addbutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { //verify pack not full int packsize = studentpack.size(); if (packsize >= 10) { joptionpane.showmessagedialog(null, "the interface full. please remove student first.", "interface full", joptionpane.warning_message); return; } jpanel p1 = new jpanel(new gridlayout(2,1)); jtextfield t1 = new jtextfield(); jlabel labell = new jlabel("please enter first name of student add:"); p1.add(labell); p1.add(t1); joptionpane.showmessagedialog(null,p1,"first name",joptionpane.plain_message); string userfirstname = t1.gettext(); jpanel p2 = new jpanel(new gridlayout(2,1)); jtextfield t2 = new jtextfield(); jlabel label2 = new jlabel("please enter last name of student add:"); p2.add(label2); p2.add(t2); joptionpane.showmessagedialog(null,p2,"last name",joptionpane.plain_message); string userlastname = t2.gettext(); jpanel p3 = new jpanel(new gridlayout(2,1)); jtextfield t3 = new jtextfield(); jlabel label3 = new jlabel("please enter major of student add:"); p3.add(label3); p3.add(t3); joptionpane.showmessagedialog(null,p3,"major",joptionpane.plain_message); string usermajor = t3.gettext(); jpanel p4 = new jpanel(new gridlayout(2,1)); jtextfield t4 = new jtextfield(); jlabel label4 = new jlabel("please enter netid of student add:"); p4.add(label4); p4.add(t4); joptionpane.showmessagedialog(null,p4,"netid",joptionpane.plain_message); string usernetid = t4.gettext(); jpanel p5 = new jpanel(new gridlayout(2,1)); jtextfield t5 = new jtextfield(); jlabel label5 = new jlabel("please enter gender of student add:"); p5.add(label5); p5.add(t5); joptionpane.showmessagedialog(null,p5,"gender",joptionpane.plain_message); string usergender = t5.gettext(); jpanel p6 = new jpanel(new gridlayout(2,1)); jtextfield t6 = new jtextfield(); jlabel label6 = new jlabel("please enter uin of student add:"); p6.add(label6); p6.add(t6); joptionpane.showmessagedialog(null,p6,"uin",joptionpane.plain_message); string useruin = t6.gettext(); jpanel p7 = new jpanel(new gridlayout(2,1)); jtextfield t7 = new jtextfield(); jlabel label7 = new jlabel("please enter age of student add:"); p7.add(label7); p7.add(t7); joptionpane.showmessagedialog(null,p7,"age",joptionpane.plain_message); string userageinput = t7.gettext(); int userage = integer.parseint(userageinput); jpanel p8 = new jpanel(new gridlayout(2,1)); jtextfield t8 = new jtextfield(); jlabel label8 = new jlabel("please enter gpa of student add:"); p8.add(label8); p8.add(t8); joptionpane.showmessagedialog(null,p8,"gpa",joptionpane.plain_message); string usergpainput = t8.gettext(); double usergpa = double.parsedouble(usergpainput); //create student student student studentstudent = new student(userfirstname, userlastname, usermajor, usergpa, useruin, usernetid, userage, usergender); //store student in linked list model.addstudent(studentstudent); } }); removebutton.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { //verify pack not full if (studentpack.size() == 0) { joptionpane.showmessagedialog(null, "the interface empty. please add student first.", "interface empty", joptionpane.warning_message); return; } jpanel p9 = new jpanel(new gridlayout(2,1)); jtextfield t9 = new jtextfield(); jlabel label9 = new jlabel("please enter first name of student remove: **case sensitive**"); p9.add(label9); p9.add(t9); joptionpane.showmessagedialog(null,p9,"first name",joptionpane.plain_message); string userfirstname = t9.gettext(); jpanel p10 = new jpanel(new gridlayout(2,1)); jtextfield t10 = new jtextfield(); jlabel label10 = new jlabel("please enter last name of student remove: **case sensitive**"); p10.add(label10); p10.add(t10); joptionpane.showmessagedialog(null,p10,"first name",joptionpane.plain_message); string userlastname = t10.gettext(); //remove student student in linked list } }); exitmenuitem.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { system.exit(0); } }); //nest button , list panels in combined content panel combinedpanel.add(headerpanel, borderlayout.north); combinedpanel.add(tablepanel, borderlayout.center); combinedpanel.add(buttonpanel, borderlayout.south); //add combined content panel frame jframe.add(combinedpanel); jframe.pack(); jframe.setlocationrelativeto(null); joptionpane.showmessagedialog(null, "***click cell edit data*** \n ***click column header sort***", "how edit/sort data", joptionpane.information_message); } public class tablemodel extends abstracttablemodel { public final string[] columnnames = { "first name", "last name", "major:", "gpa", "uin", "netid", "age", "gender" }; private mylinkedlist data; public tablemodel(mylinkedlist data) { this.data = data; } @override public int getcolumncount() { return columnnames.length; } @override public string getcolumnname(int column) { return columnnames[column]; } @override public int getrowcount() { return data.size(); } public void addstudent(student student) { int row = data.size(); data.add(student); firetablerowsinserted(row, row); } public void removestudent(int i) { int row = data.size(); data.remove(i); firetablerowsdeleted(row, row); } @override public student getvalueat(int rowindex, int columnindex) { object student = data.getnode(rowindex); if (student == null) { return null; } switch (columnindex) { case 0: return student.getfirstname(); case 1: return student.getlastname(); case 2: return student.getmajor(); case 3: return student.getgpa(); case 4: return student.getuin(); case 5: return student.getnetid(); case 6: return student.getage(); case 7: return student.getgender(); default: return null; } } } }
here's class student object:
public class student { // declare attributes here string firstname; string lastname; string major; double gpa; string uin; string netid; int age; string gender; public student(string newfirstname, string newlastname, string newmajor, double newgpa, string newuin, string newnetid, int newage, string newgender){ setfirstname(newfirstname); setlastname(newlastname); setmajor(newmajor); setgpa(newgpa); setuin(newuin); setnetid(newnetid); setage(newage); setgender(newgender); } //mutators public void setfirstname(string newfirstname) { firstname = newfirstname; } public void setlastname(string newlastname) { lastname = newlastname; } public void setmajor(string newmajor) { major = newmajor; } public void setgpa(double newgpa) { gpa = newgpa; } public void setuin(string newuin) { uin = newuin; } public void setnetid(string newnetid) { netid = newnetid; } public void setage(int newage) { age = newage; } public void setgender(string newgender) { gender = newgender; } //accessors public string getfirstname() { return firstname; } public string getlastname() { return lastname; } public string getmajor() { return major; } public double getgpa() { return gpa; } public string getuin() { return uin; } public string getnetid() { return netid; } public int getage() { return age; } public string getgender() { return gender; } }
here's reworked linked list class:
public class mylinkedlist<student>{ private node<student> lastnode; private int nodecount; public mylinkedlist(){ this.lastnode = null; this.nodecount = 0; } public int size() { return nodecount; } public boolean isempty() { return this.nodecount == 0; } public void add(student student) { node<student> currentnode = new node<student>(student); if (this.lastnode != null) { currentnode.index = lastnode.index + 1; currentnode.previousnode = lastnode; lastnode.nextnode = currentnode; }else { currentnode.previousnode = null; currentnode.index = 0; } this.lastnode = currentnode; this.nodecount++; } public student get(int index){ //error handling if(this.isempty() || index < 0 || index > this.nodecount){ return null; } // node<student> currentnode; int count = lastnode.index - index; currentnode = lastnode; while (count > 0) { currentnode = currentnode.previousnode; count--; } return currentnode.student; } public node<student> getnode(int index){ //error handling if(this.isempty() || index < 0 || index > this.nodecount){ return null; } // int count = lastnode.index - index; node<student> currentnode = lastnode; while (count > 0){ currentnode = currentnode.previousnode; count--; } return currentnode; } public boolean insert(student student, int index){ node<student> currentnode; if (this.getnode(index) != null) { node<student> newnode = new node<student>(student); currentnode = this.getnode(index); newnode.index = index; if ( null != currentnode.previousnode ) { currentnode.previousnode.nextnode = newnode; newnode.previousnode = currentnode.previousnode; } currentnode.previousnode = newnode; newnode.nextnode = currentnode; currentnode = newnode; while (currentnode.nextnode != null) { currentnode = currentnode.nextnode; currentnode.index++; } this.nodecount++; return true; } else { return false; // error handling } } public boolean remove(int index){ node<student> currentnode; if (this.getnode(index) != null){ currentnode = this.getnode(index); if(currentnode.previousnode != null) { currentnode.nextnode.previousnode = currentnode.previousnode; currentnode.previousnode.nextnode = currentnode.nextnode; } else if (currentnode.nextnode != null){ currentnode.nextnode.previousnode = null; } else if (this.isempty()){ this.lastnode = null; } while (currentnode.nextnode != null){ currentnode = currentnode.nextnode; currentnode.index--; } this.nodecount--; return true; }else { return false; } } private static class node<student>{ public node<student> nextnode = null; public node<student> previousnode = null; public int index; public student student; public node(student student){ this.student = student; } } }
this...
public class tablemodel extends abstracttablemodel { //... @override public student getvalueat(int rowindex, int columnindex) { object student = data.getnode(rowindex); if (student == null) { return null; } switch (columnindex) { case 0: return student.getfirstname(); case 1: return student.getlastname(); case 2: return student.getmajor(); case 3: return student.getgpa(); case 4: return student.getuin(); case 5: return student.getnetid(); case 6: return student.getage(); case 7: return student.getgender(); default: return null; } } }
is giving me compiler error, because student
object
, object
not have of methods trying use.
the obvious fix use...
@override public student getvalueat(int rowindex, int columnindex) { student student = data.getnode(rowindex);
okay, there 2 problems. first getvalueat
shouldn't returning student
, should returning object
, 2 getnode
returns node<student>
so instead, should more like...
@override public object getvalueat(int rowindex, int columnindex) { student student = data.get(rowindex);
okay, causing compiler error, why? get
returns student
?
i tracked problem down public class mylinkedlist<student> {
... instead, if make public class mylinkedlist {
errors go away.
your mylinkedlist
doesn't need generic signature <student>
, compiler seeing "generic" token , replacing object
in code, get
method became public object get(int index)
instance of mylinkedlist
didn't provide base object start ... yea generics :p
now, end can generate...
Comments
Post a Comment