Reputation: 2584
I'm having a problem with separating The Table Model(in the Model) from my JTable (in the view), My model contains the query needed to get the data from the database with a buildTableModel()
method:
Model.java
public static DefaultTableModel buildTableModel(ResultSet rs)
throws SQLException {
ResultSetMetaData metaData = rs.getMetaData();
// names of columns
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount; column++) {
columnNames.add(metaData.getColumnName(column));
}
// data of the table
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
}
//queries to execute for Jtable model listProduit
public ResultSet productList()throws SQLException{
ResultSet rs = stmt.executeQuery("SELECT * from Lot");
return rs;
}
View.java
public class View extends JPanel{
public DefaultTableModel dt;
public JTable productTable;
private JScrollPane scrolPane;
public ListProduit(){
productTable=new JTable(dt);
this.setLayout(new GroupLayout(this));
this.setBackground(Color.decode("#CFDBC5"));
productTable.setPreferredScrollableViewportSize(new Dimension(500,50));
productTable.setFillsViewportHeight(true);
scrolPane= new JScrollPane(productTable);
this.add(scrolPane);
this.setVisible(true);
}
//setter for the table model
public void setDt(DefaultTableModel dt) {
this.dt = dt;
this.dt.fireTableDataChanged();
}
And this is the part where I'm getting the problem:
Controller.java
try{ //lines of the problem:
ResultSet rs= model.productList();
DefaultTableModel dtm = Model.buildTableModel(rs);
View.setDt(dtm);
// Stuff to handle showing the view
showFourthCard();
panelList.add(4);
}catch(SQLException ex){
ex.printStackTrace();
}
Apparentlly the JTable
can not change the DefaultTbaleModel
object that it gets in the first time so when I'm executing I always get a blank JTable, so in short I cannot set a new DefaultTableModel
object in my Controller.java
.
Note: This works just fine when I'm not using MVC
(because I don't have to set the Table Model), so my problem here is mainly with the separation of the JTable from the Table Model.
Upvotes: 0
Views: 751
Reputation: 8348
Apparentlly the JTable can not change the DefaultTbaleModel object that it gets in the first time so when I'm executing I always get a blank JTable, so in short I cannot set a new DefaultTableModel object in my Controller.java.
public void setDt(DefaultTableModel dt) {
this.dt = dt;
this.dt.fireTableDataChanged();
}
The code above only changes the field, it does not change the TableModel of the JTable
instance (eg productTable
, which is initially set in the constructor). Make a call to the JTable
instance itself to actually change the model backing the JTable
eg productTable.setModel(dt);
Upvotes: 2