java JTable 已替换(?)但尽管重新验证和重绘仍未刷新

java JTable replaced (?) but not being refreshed despite revalidate and repaint

有如下 SSCCE,假设您在 GNU/Linux,请看看您是否可以在下面回答我的问题。将两个文件保存到“/tmp/aaa/some/packagepackagea/Test.java”和“/tmp/aaa/some/packagepackagea/DataHandler.java”或修复路径。 我已经测试过它可以编译和运行。

请用您的数据填写 table,或按 "save"(硬编码路径 ("/tmp/aaa/testdata"))

  1. 我想知道为什么我的 JTable 不检索存储的内容?
    是因为final JTable table吗?如果我理解正确,如果 table 是最终的,那么 d.load("/tmp/aaa/test", table, ";"); 将副本(或引用?)传递给 DataHandler (d) 加载函数。在这个函数中,我尝试分配给最终变量(?)或者更确切地说是在函数退出后得到 GC 的引用,所以这就是我的 table 在加载后什么都没有的原因?

Test.java

package some.packagepackagea;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;
import java.text.*;

public class Test {

    Test () {

        final DataHandler d = new DataHandler();

        JFrame root = new JFrame("zxczxc");
        root.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final javax.swing.table.DefaultTableModel model = new javax.swing.table.DefaultTableModel();
        final JPanel x = new JPanel();
        final JTable table = new JTable(model);
        JScrollPane table_container = new JScrollPane(table);

        // read some columns from file/db
        final String cols[] = {"aaaa", "bbbbb", "ccccc", "ddddd", "eeee"};
        for (String col:cols)
            model.addColumn(col);

        Object y[] = new Object[]{"a", "b"};
        model.addRow(y);
        model.addRow(y);
        JButton a = new JButton("save");
        JButton b = new JButton("load");

        x.add(a);
        x.add(b);
        x.add(table_container);

        a.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                d.save(table, "/tmp/aaa/testdata", ";");
                x.revalidate();
                x.repaint();
            }
         });
        b.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                d.load("/tmp/aaa/testdata", table, ";");
                model.fireTableDataChanged();
                x.revalidate();
                x.repaint();
            }
         });
        root.add(x);
        root.pack();                            // shrink borders
        root.setLocationRelativeTo(null);       // center panel
        root.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Test it = new Test();
            }
        });
    }
}

DataHandler.java

package some.packagepackagea;
import java.io.*;
import java.nio.CharBuffer;
import javax.swing.*;
import javax.swing.table.*;
import java.util.ArrayList;

public class DataHandler {

    public void save(JTable table, String file, String separator){
        File f = new File(file);
        save(table, f, separator);
    }

    public void save(JTable table, File file, String separator){

        try {
            TableModel tableModel = table.getModel();
            FileWriter disk = new FileWriter(file);

            for(int i = 0; i < tableModel.getColumnCount(); i++){
                disk.write(tableModel.getColumnName(i) + separator);
            }
            disk.write("\n");

            for(int i=0; i< tableModel.getRowCount(); i++) {
                for(int j=0; j < tableModel.getColumnCount(); j++) {
                    Object item = tableModel.getValueAt(i,j);
                    if (null != item)
                        disk.write(item.toString() + separator);
                }
                disk.write("\n");
            }
            disk.close();

            ((DefaultTableModel)tableModel).getDataVector().removeAllElements();

        } catch (IOException e){ System.out.println(e); }
    }

    public void load(String file, JTable table, String separator){
        File f = new File(file);
        load(f, table, separator);
    }

    public void load(File file, JTable table, String separator){

        CharBuffer buf = CharBuffer.allocate((int)file.length());
        try {
            FileReader disk = new FileReader(file);
            disk.read(buf);
            buf.rewind();
            disk.close();

        } catch (IOException e){ System.out.println(e); }

        String rows[] = (buf.toString()).split("\n");
        ArrayList<String[]> cols = new ArrayList<String[]>();
        for(String row:rows){
            cols.add(row.split(separator));
        }

        // it might be more efficient to return model and initialize table with
        // it but i find it nicer when methods are somewhat symmetric.
        TableModel tableModel = new DefaultTableModel();
        for(String[] col:cols){
            ((DefaultTableModel)tableModel).addRow(col);
        }
        table = new JTable(tableModel);
        ((DefaultTableModel)tableModel).fireTableDataChanged();
    }
}

编辑:

我也试过替换

 table = new JTable(tableModel);

 table.setModel(tableModel);

但这并没有达到目的。

您正在创建一个新的 table,它不会被 GUI 所引用。

所以稍微重写一下加载方法,您可以在 table 上设置模型,像这样:

String rows[] = (buf.toString()).split("\n");

DefaultTableModel tableModel = new DefaultTableModel();

for (String col:rows[0].split(separator))
    tableModel.addColumn(col);

for (int i = 1; i < rows.length; i++)
    tableModel.addRow(rows[i].split(separator));

table.setModel(tableModel);

第二件事是你的保存方法。将其输出到磁盘时,您正在删除 null 值。而不是:

Object item = tableModel.getValueAt(i,j);
if (null != item)
    disk.write(item.toString() + separator);

做这样的事情:

Object item = tableModel.getValueAt(i,j);
disk.write((item != null ? item.toString() : "") + separator);