java - Refreshing JTable data using JcomboBox items coming from Access Database -
how refresh jtable ?
here code.
public void itemstatechanged(itemevent evt) { string text=(string)to_cmb2.getselecteditem(); try { // connect access database connection con=drivermanager.getconnection("jdbc:odbc:flightdsn"); statement s=con.createstatement(); // read data table resultset rs = s.executequery("select flightno,city,to,arrives,departs i_flights_routes ='"+text+"' "); resultsetmetadata md = rs.getmetadata(); int columns = md.getcolumncount(); // column names (int = 1; <= columns; i++) { columnnames.addelement(md.getcolumnname(i)); } // row data while (rs.next()) { vector<object> row = new vector<>(columns); (int = 1; <= columns; i++) { row.addelement(rs.getobject(i)); } data.addelement(row); } rs.close(); s.close(); con.close(); } catch (exception e) { system.out.println(e); } // create table database data jtable table = new jtable(data, columnnames) { public class getcolumnclass(int column) { (int row = 0; row < getrowcount(); row++) { object o = getvalueat(row, column); if (o != null) { return o.getclass(); } } return object.class; } }; jscrollpane scrollpane = new jscrollpane(table); scrollpane.setbounds(50,10,400,200); td.add(scrollpane); // td jpanel object td.setvisible(true); }
you're creating entirely new jtable -- don't that. use same jtable
- modify jtable's model if want add new data existing data
- or if want totally replace data in table, create new defaulttablemodel (or other tablemodel) , set jtable's model via `setmodel(...).
also, aside want avoid using null layouts , setbounds(...)
creates inflexible gui's terrible on other platforms or other resolutions , difficult upgrade , manage.
Comments
Post a Comment