i have a jtable as shown in the pictures with 2 JDateChooser when i search and populate the table it loses the cell render is there a way to fix it ? i have tried alot of things suggested but none worked
this is the action for the button for searching and seting the mode
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
java.sql.Date lowerdate = new java.sql.Date(dateChooser.getDate().getTime());
java.sql.Date upperdate = new java.sql.Date(dateChooser_1.getDate().getTime());
Connection con = DerbyConnection.dbConnector();
PreparedStatement pstmt = con.prepareStatement("select * from COMPANYCHEKS WHERE CHEKSDATEIN >= ? AND CHEKSDATEIN <= ?");
pstmt.setDate(1, lowerdate);
pstmt.setDate(2,upperdate);
ResultSet rs = pstmt.executeQuery();
table.setModel(Menu32.resultSetToTableModel(rs));
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
here is my model set to be called for populating the table with the result
public static TableModel resultSetToTableModel(ResultSet rs) {
try {
ResultSetMetaData metaData = rs.getMetaData();
int numberOfColumns = metaData.getColumnCount();
Vector<String> columnNames = new Vector<String>();
columnNames.add("الرمز");
columnNames.add("العميل");
columnNames.add("البنك");
columnNames.add("تاريخ الشك");
columnNames.add("رقم الشك");
columnNames.add("القيمة");
columnNames.add("العملة");
columnNames.add("سعر الصرف");
columnNames.add("القيمة بالدولار");
columnNames.add("تاريخ الإيداع");
columnNames.add("بنك الإيداع");
columnNames.add("الحالة");
columnNames.add("ملاحظات");
// Get all rows.
Vector<Vector<Object>> rows = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> newRow = new Vector<Object>();
for (int i = 1; i <= numberOfColumns; i++) {
newRow.addElement(rs.getObject(i));
}
rows.addElement(newRow);
}
return new DefaultTableModel(rows, columnNames);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
here you can see what happens
Table before search:
table after populate:
when i search and populate the table it loses the cell render is there a way to fix it ?
By default, when you create set a new model to the table, a new TableColumnModel is created based on the new data in the TableModel and you lose the custom renderers.
Assuming the columns of the model are the same and only the rows of data changed you can prevent the TableColumnModel from be creating by invoking:
table.setAutoCreateColumnsFromModel( false );
after you have created the JTable with the original TableModel.