Hello,
Im trying to create a "Header" for my JTable by using .setModel()...
But the problem is that it doesn't work.. My Jtable gets it's data from Db and when I run it the .setModel() hides the data from Dd. After my function Update() the data will show up.
This s my code for getting the data from my db:
Code for creating JTable and .setModel()try { ResultSet result = statement.executeQuery("SELECT * FROM devices"); java.sql.ResultSetMetaData md = result.getMetaData(); int columnCount = md.getColumnCount(); colums = new Vector(columnCount); for(int i=1;i<=columnCount;i++){ colums.add(md.getColumnCount()); } data = new Vector(); while(result.next()){ Vector row = new Vector(columnCount); for(int i=1;i<=columnCount;i++){ row.add(result.getString(i)); } data.add(row); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
table = new JTable(data,colums); [ATTACH=CONFIG]1427[/ATTACH] table.setModel(new DefaultTableModel( new Object[][] { {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}, {null, null, null}, }, new String[] { "Device Name", "Device State", "Id" } )); getContentPane().add(table);
My update function:
This is the first problem.protected void Update_Table() { // TODO Auto-generated method stub try{ String update = "SELECT * FROM devices"; // statUp = (Statement) con.createStatement(update); ResultSet rest = statUp.executeQuery(update); //table.setModel(DbUtils.updateTableModel(rest)); table.setModel(DbUtils.resultSetToTableModel(rest)); //DbUtils. } catch(Exception ex){ ex.printStackTrace(); JOptionPane.showInputDialog(null, "Couldn't create new Device"); } }
Second problem is that even like this the name of the columns doesnt appear.
Can you please help me?
Thank you