java JTable已替换(?)但未刷新,尽管重新验证并重新绘制

时间:2015-11-20 11:00:48

标签: java swing jtable tablemodel

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

请在表格中填入您的数据,或按“保存”(硬编码路径(“/ tmp / aaa / testdata”))

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

    但这也没有做到这一点。

1 个答案:

答案 0 :(得分:2)

您正在创建一个未替换GUI引用的新表。

所以重写一下load方法,你可以在桌面上设置模型,如下所示:

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