【问题标题】:how can I "refresh" my table, so that I could display same table with different data?如何“刷新”我的表格,以便我可以显示具有不同数据的同一张表格?
【发布时间】:2015-07-03 07:37:25
【问题描述】:

如何“刷新”我的表格,以便我可以显示具有不同数据的同一张表格?

String columnNames[] = {"First Name", "Last Name", "Phone Number", "E-mail"};
    JTable contactTable = new JTable(data, columnNames);
    jScrollPane = new JScrollPane(contactTable,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

【问题讨论】:

标签: java swing


【解决方案1】:

您的数据保存在哪里?

我将它保存在一个文本文件中并使用这种方式来刷新我的数据,但每次都需要一个按钮来刷新

这是我的代码

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // Refresh Button

        try {
            for (int r = 0; r < 100; r++) { //initializing row
                for (int c = 0; c < 4; c++) { //initializing column
                    jTable1.setValueAt(null, r, c);
                }
            }

            BufferedReader rdfile = new BufferedReader(new FileReader("items.txt"));

            String[] item = new String[100];
            String[] temp;

            int x = 0;  //read item
            while ((item[x] = rdfile.readLine()) != null) {
                temp = item[x].split("\t");
                jTable1.setValueAt((1000 + x + 1), x, 0);
                for (int j = 1; j < 4; j++) {
                    jTable1.setValueAt(temp[j - 1], x, j);
                }

                x++;
            }
            rdfile.close();

        } catch (IOException e) {
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    相关资源
    最近更新 更多