简体   繁体   English

JTable设置模型并保留列格式(宽度,对齐等)

[英]JTable setting model and preserve column formats (width, alignment, etc.)

This is a brain-cracking experience with JTable binding. 这是使用JTable绑定的大脑破解体验。 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. 我创建了一个JTable ,其列设置为指定的宽度,使用renderers对其进行格式化,并在其上添加了一些代码。 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. 我正在避免循环,因为数据库中有超过100条记录。 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. 我正在尝试使用其他方法,如BeansBindingEntityManager但我不知道如何更改数据源( 这就是我选择将其绑定到模型的原因),因为我正在测试这个备份数据库,很快就会实现新服务器。

This is what I did in .Net , I usually create a datagridview and binded it to a dataset and it works fine. 这就是我在.Net所做的,我通常创建一个datagridview并将其绑定到数据集,它工作正常。 But i cannot apply it to java. 但我无法将其应用于java。 I need your opinion on how can I do it in java that can handle 100+ thousand records. 我需要你的意见,我怎么能在java中做到这一点,可以处理100多万条记录。

    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. 请帮帮我。

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 1)从来没有,真的从来没有把100+ thousand records放到视图中,这对所有编程语言都有效,真的没用

2) (my view) BeansBindings are out_dated use standard TableModel instead 2)(我的观点)BeansBindings是out_dated使用标准的TableModel代替

3) nothing better as @camickr TableFromDatabase is around, or another suggestion is searching for ResultSetTableModel 3)没有更好的@camickr TableFromDatabase ,或另一个建议是搜索ResultSetTableModel

Based on the comments, you could try to add the following method to DbUtils: 根据注释,您可以尝试将以下方法添加到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);

l have discovered that you can create your formatting method and call it after the try block as below. 我发现您可以创建格式化方法并在try块之后调用它,如下所示。

 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 这将完美地工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM