JTable设置模型并保留列格式(宽度,对齐等) [英] JTable setting model and preserve column formats (width, alignment, etc.)

查看:156
本文介绍了JTable设置模型并保留列格式(宽度,对齐等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个使用 JTable 绑定的大脑破解体验。这就是我做的。我创建了一个 JTable ,其列设置为指定的宽度,使用 renderers 对其进行格式化,并在其上添加了一些代码。 但是当我尝试将其绑定到模型时,所有列都被模型的字段 替换。有没有办法如何正确绑定它?



我正在避免循环,因为数据库中有超过100条记录。我正在尝试使用其他方法,如 BeansBinding EntityManager 但我不知道如何更改数据源(这就是为什么我选择将它绑定到模型),因为我正在测试它到备份数据库,很快就会实现到新服务器。



<这是我在 .Net 中所做的,我通常创建一个datagridview并将其绑定到数据集,它工作正常。但我无法将其应用于java。我需要你的意见,我怎么能用java来处理超过100万条记录。

  PreparedStatement pst = conn.prepareStatement (SQL Query here); 
ResultSet rs = pst.ExecuteQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));

上面的代码工作正常,但我在这方面的一个非常大的问题是它覆盖了我的格式化列模型中的列。



请帮我解决这个问题。

解决方案

根据评论,你可以尝试将以下方法添加到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);
}
}

然后您的代码将成为:

  PreparedStatement pst = conn.prepareStatement(SQL Query here); 
ResultSet rs = pst.ExecuteQuery();
DbUtils.updateTableModelData((DefaultTableModel)jTable1.getModel(),rs);


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.

解决方案

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);

这篇关于JTable设置模型并保留列格式(宽度,对齐等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆