java - AbstractTabelModel won't show the JTable column names -
for reason jtable not displaying it's column names?! i'm i've done correctly. i've literally copied demonstration don't understand why won't work.
here code:
public class memtablemodel extends abstracttablemodel{ private arraylist<member> members = new arraylist<member>(); private string[] columnnames = {"id", "name", "email", "country", "genre", "gender", "description", "type", "limit", "card no", "expiry date"}; public memtablemodel(){ loadtablefromdb(); } public int getrowcount(){ return members.size(); } public int getcolumncount(){ return columnnames.length; } public object getvalueat(int row, int col){ //get row method member f = members.get(row); switch(col){ case 0: return f.getmembid(); case 1: return f.getname(); case 2: return f.getemail(); case 3: return f.getcountry(); case 4: return f.getfavgenre(); case 5: return f.getgender(); case 6: return f.getdescription(); case 7: return f.getmembertype(); case 8: return f.getsonglimit(); case 9: return f.getcard_no(); case 10: return f.getexpiry_date(); } return null; } public string getcolumnname(int col){ return columnnames[col]; } public member getrow(int row){ member c = members.get(row); return c; } public connection getconnection(){ connection condb = null; /****** default mysql drivers **************************/ string url = connection.geturl(); string username = connection.getusername(); string password = connection.getpassword(); try{ //load mysql driver class.forname(connection.getdriver()); condb = drivermanager.getconnection(url, username, password); } catch(exception e){ } return condb; } //load db values array public void loadtablefromdb(){ connection condb = null; statement stmt = null; resultset r = null; try{ //connection + statement condb = getconnection(); stmt = condb.createstatement(); //queries string sqlselectall = "select * members"; r = stmt.executequery(sqlselectall); members.clear(); //loop through resultset while(r.next()){ members.add(new member(r.getint("membid"), r.getstring("name"), r.getstring("email"), r.getstring("country"), r.getstring("favgenre"), r.getstring("gender"), r.getstring("description"), r.getstring("membertype"), r.getstring("songlimit"), r.getstring("card_no"), r.getstring("expiry_date"))); } condb.close(); // close db connection }//end of try catch(exception er){ system.out.println("error was: " + er); } } }
here how i've implemented jtable:
public class viewall extends jframe implements actionlistener{ //jtextfields, buttons, labels private jbutton btnback = new jbutton("back"); private static jlabel lblmembtitle = new jlabel("<html><h1>all members</h1></html>"); private static jlabel lblplaytitle = new jlabel("<html><h1>all playlists</h1><br /></html>"); //containers, panels, scrollpanes private container maincon = this.getcontentpane(); private static jpanel pnltable = new jpanel(new borderlayout()); //jpanels - sections private jpanel mainpanel = new jpanel(); private jpanel subpanel1 = new jpanel(); private jpanel subpanel2 = new jpanel(); private jpanel subpanel3 = new jpanel(); //tables private static jtable tblshowallmemb = new jtable(); private static jtable tblshowallplay = new jtable(); jscrollpane scrollpane = new jscrollpane(mainpanel, scrollpaneconstants.vertical_scrollbar_as_needed, scrollpaneconstants.horizontal_scrollbar_as_needed); public viewall(){ super("search/edit/delete members"); this.setbounds(400, 800, 854,400); this.setvisible(true); maincon.add(scrollpane); //table models: memtablemodel tblmembers = new memtablemodel(); playtablemodel tblplaylist = new playtablemodel(); //layout /*by removing scrollpane works ^^mainpanel added scrollpane object above ^^ */ // maincon.add(mainpanel); //main panel mainpanel.setlayout(new borderlayout()); mainpanel.add(borderlayout.north, subpanel1); mainpanel.add(borderlayout.center, subpanel2); //panel1 - member table + button subpanel1.setlayout(new borderlayout()); subpanel1.add(borderlayout.north, btnback); subpanel1.add(borderlayout.center, lblmembtitle); subpanel1.add(borderlayout.south, tblshowallmemb); tblshowallmemb.setmodel(tblmembers); btnback.addactionlistener(this); //panel2 - playlist table subpanel2.add(borderlayout.north, lblplaytitle); subpanel2.add(borderlayout.center, tblshowallplay); tblshowallplay.setmodel(tblplaylist); } @override public void actionperformed(actionevent e) { if(e.getsource() == btnback){ this.dispose(); } } }
the issue you've not wrapped jtable
, represents tablemodel
in jscrollpane
demonstrated in how use tables
by using like...
add(new jscrollpane(new jtable(new memtablemodel())));
i can get:
see how use scroll panes more details
updated based on updated code...
not 1 of tables wrapped within it's own jscrollpane
// way, static here very, bad idea private static jtable tblshowallmemb = new jtable(); private static jtable tblshowallplay = new jtable(); jscrollpane scrollpane = new jscrollpane(mainpanel, scrollpaneconstants.vertical_scrollbar_as_needed, scrollpaneconstants.horizontal_scrollbar_as_needed); public viewall(){ //.... //table models: memtablemodel tblmembers = new memtablemodel(); playtablemodel tblplaylist = new playtablemodel(); //... subpanel1.add(borderlayout.south, tblshowallmemb); //... subpanel2.add(borderlayout.center, tblshowallplay);
you've added table other container. instead, consider using like
//... subpanel1.add(borderlayout.south, new jscrollpane(tblshowallmemb)); //... subpanel2.add(borderlayout.center, new jscrollpane(tblshowallplay));
Comments
Post a Comment