问题
This is a brain-cracking experience with JTable
binding. Here's what I did. I created a JTable
with columns set to a specified width, formatted it using renderers
, and added some codes on it. But when i try to bind it to a model, all the columns were replaced by the fields of the model. Is there a way on how to correctly bind it?
I'm avoiding loops because there are 100+ thousand records in the database. I'm trying to use other methods such as BeansBinding
and EntityManager
but I don't know how to change the datasource (that's why i choose binding it to a model) because I'm testing this to backup database and soon to be implemented to the new server.
This is what I did in .Net
, I usually create a datagridview and binded it to a dataset and it works fine. But i cannot apply it to java. I need your opinion on how can I do it in java that can handle 100+ thousand records.
PreparedStatement pst = conn.prepareStatement("SQL Query here");
ResultSet rs = pst.ExecuteQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
The code above works fine but my very very big problem on this is that it overrides my formatted columns by the columns in the model.
Please help me on this.
回答1:
Based on the comments, you could try to add the following method to DbUtils:
public static void updateTableModelData(DefaultTableModel tModel, ResultSet rs)
throws Exception {
tModel.setRowCount(0);
ResultSetMetaData metaData = rs.getMetaData();
while (rs.next()) {
Vector newRow = new Vector();
for (int i = 1; i <= numberOfColumns; i++) {
newRow.addElement(rs.getObject(i));
}
tModel.addRow(newRow);
}
}
and then your code would became:
PreparedStatement pst = conn.prepareStatement("SQL Query here");
ResultSet rs = pst.ExecuteQuery();
DbUtils.updateTableModelData((DefaultTableModel) jTable1.getModel(), rs);
回答2:
too wide question
1) never, really never put 100+ thousand records
to the view, and this is valid for all programing languages, is really useless
2) (my view) BeansBindings are out_dated use standard TableModel instead
3) nothing better as @camickr TableFromDatabase is around, or another suggestion is searching for ResultSetTableModel
回答3:
l have discovered that you can create your formatting method and call it after the try block as below.
private void UpdateTable() throws SQLException {
String sql = "select * from invoice";
proDialog.setValue(10);
table.setShowHorizontalLines(true);
table.setShowVerticalLines(true);
proDialog.setValue(20);
try (
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
)
{
table.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
format();
}
public void format() {
TableColumnModel m = table.getColumnModel();
m.getColumn(2).setCellRenderer(FormatRenderer.getDateTimeRenderer());
m.getColumn(1).setCellRenderer(FormatRenderer.getTimeRenderer());
m.getColumn(2).setCellRenderer(NumberRenderer.getPercentRenderer());
m.getColumn(4).setCellRenderer(NumberRenderer.getCurrencyRenderer());
m.getColumn(4).setCellRenderer(NumberRenderer.getPercentRenderer());
m.getColumn(5).setCellRenderer(NumberRenderer.getCurrencyRenderer());
}
This will work perfectly
来源:https://stackoverflow.com/questions/9599474/jtable-setting-model-and-preserve-column-formats-width-alignment-etc