java - Add user input data to an ArrayList and display it into a Jtable using custom a Tablemodel -
java - Add user input data to an ArrayList and display it into a Jtable using custom a Tablemodel -
i'm having problems getting updated , displayed array list of people(phonebook contacts). seek add together user input info array list using jbutton , displayed in jtable.
i appreciate if can tell me if there design or implementation problem. give thanks in advance suggestions!
here part of code
custom table model
package cartedetelefon.gui; import static cartedetelefon.cartedetelefon.listacontacte; import cartedetelefon.exceptii.exceptiecontactdejadefinit; import cartedetelefon.nrtel; import javax.swing.event.tablemodelevent; import javax.swing.event.tablemodellistener; import javax.swing.table.abstracttablemodel; public class tblmodel extends abstracttablemodel implements tablemodellistener{ nrtel[] contacte = new nrtel[0]; @override public int getrowcount() { homecoming listacontacte.size(); } @override public int getcolumncount() { homecoming 6; } @override public object getvalueat(int linie, int coloana) { nrtel contact = contacte[linie]; switch (coloana){ case 0: homecoming contact; case 1: homecoming contact.getpersoana().getnume(); case 2: homecoming contact.getpersoana().getprenume(); case 3: homecoming contact.getnrmobil(); case 4: homecoming contact.getnrbirou(); case 5: homecoming contact.getnrhome(); default: homecoming "eroare"; } } public void adaugarecontact(nrtel c) throws exceptiecontactdejadefinit { if (listacontacte.contains(c)) throw new exceptiecontactdejadefinit("datele de contact pentru "+c.getpersoana()+ " exista!"); listacontacte.add(c); firetablerowsinserted(contacte.length-1, contacte.length-1); } @override public string getcolumnname(int coloana) { homecoming new string[] {"nume", "prenume", "cnp", "mobil", "birou", "resedinta"}[coloana]; } @override public void tablechanged(tablemodelevent tme) { int row = tme.getfirstrow(); int column = tme.getcolumn(); tblmodel model = (tblmodel)tme.getsource(); string coloana = model.getcolumnname(column); object info = model.getvalueat(row, column); } }
phonebook class
public class cartedetelefon{ public static list<nrtel> listacontacte = new arraylist<>(); public collection<nrtel> gettoatecontactele() { homecoming listacontacte; }
gui
package cartedetelefon.gui; import cartedetelefon.cartedetelefon; import static cartedetelefon.cartedetelefon.listacontacte; import cartedetelefon.nrtel; import java.awt.borderlayout; import java.awt.component; import java.awt.dimension; import java.awt.frame; import java.awt.toolkit; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectinputstream; import java.io.objectoutputstream; import java.util.collection; import java.util.collections; import java.util.list; import java.util.logging.level; import java.util.logging.logger; import javax.swing.imageicon; import javax.swing.jfilechooser; import javax.swing.jlabel; import javax.swing.joptionpane; import javax.swing.jtable; import javax.swing.jtextarea; import javax.swing.jwindow; import javax.swing.swingutilities; import javax.swing.event.tablemodellistener; public class gui extends javax.swing.jframe { static private final string newline = "\n"; @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents(){} private void adaugaactionperformed(java.awt.event.actionevent evt) { **// todo add together handling code here:** // possible solution don't know how link arraylist object row[]=new object[6]; while((row[0]=joptionpane.showinputdialog("nume:")).equals("")); while((row[1]=joptionpane.showinputdialog("prenume:")).equals("")); while((row[2]=joptionpane.showinputdialog("cod numeric personal:")).equals("")); while((row[3]=joptionpane.showinputdialog("numar telefon mobil:")).equals("")); while((row[4]=joptionpane.showinputdialog("numar telefon birou:")).equals("")); while((row[5]=joptionpane.showinputdialog("numar telefon resedinta:")).equals("")); //model.adaugarecontact(new nrtel(null, newline, newline, newline, newline)); not working } jtextarea log; private tablemodellistener tml; /** * creates new form gui */ public gui() { initcomponents(); tblmodel model = new tblmodel(); listaabonatigui.setmodel(model); listaabonatigui.getmodel().addtablemodellistener(tml); } public static void main(string args[]){ cartedetelefon pb = new cartedetelefon(); collection<nrtel> contacte = pb.gettoatecontactele(); collections.sort((list<nrtel>) contacte); system.out.println("contacte cu detalii " +contacte); /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { @override public void run() { new gui().setvisible(true); } }); }
i seek add together user input info array list using jbutton , displayed in jtable.
that wrong approach. tablemodel should hold data. need method add together row of info tablemodel
nrtel[] contacte = new nrtel[0];
that makes no sense trying create array hold info has 0 row. should not utilize array hold info since don't know how many rows should added. instead should utilize arraylist hold nrtel objects. every time add together row model add together object arraylist.
take @ row table model generic implementation of custom model. still need implement couple of methods back upwards nrtel object. jbuttontablemode.java
shows how might implement these methods, alter code class.
java swing user-interface arraylist jtable
Comments
Post a Comment