将 Interactive JTable 的内容保存到 .txt 文件以在下次运行时读取它
Posted
技术标签:
【中文标题】将 Interactive JTable 的内容保存到 .txt 文件以在下次运行时读取它【英文标题】:Saving content of Interactive JTable to .txt file to read it upon next run 【发布时间】:2015-07-15 13:59:29 【问题描述】:大家好,我目前正在编写一个向用户显示 JTable 的程序,以便他们可以在表格窗口中输入一些数据。问题是,我不知道如何保存任何数据,所以当应用程序再次运行时可以重新打开数据。我希望在应用关闭时自动保存数据,但如果需要,我很乐意使用按钮。
【问题讨论】:
尝试序列化TableModel
而不是组件。如果您在执行此操作时遇到问题,请准备一份您的最佳尝试MCVE 并提出具体问题。
【参考方案1】:
我不知道如何保存任何数据,
从 JTable API(和所有 Swing 组件)您将看到:
警告:此类的序列化对象将与未来的 Swing 版本不兼容......从 1.4 开始,对所有 JavaBeans™ 的长期存储的支持已添加到 java.beans 包中。请参阅 XMLEncoder。
这是一个使用 XMLEncoder 的示例:
// Following code is a more complete version of:
// http://***.com/q/26250939/131872
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.io.*;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.*;
public class DefaultTableModelPersistenceDelegateTest
private File file = new File("TableModel.xml");
private final JTextArea textArea = new JTextArea();
private final String[] columnNames = "Column1", "Column2";
private final Object[][] data =
"aaa", new Integer(1),
"bbb\u2600", new Integer(2)
;
private DefaultTableModel model = new DefaultTableModel(data, columnNames);
private final JTable table = new JTable(model);
public JComponent makeUI()
model.setColumnCount(5);
JSplitPane sp = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
sp.setResizeWeight(.3);
sp.setTopComponent(new JScrollPane(table));
sp.setBottomComponent(new JScrollPane(textArea));
JPanel p = new JPanel();
p.add(new JButton(new AbstractAction("XMLEncoder")
@Override
public void actionPerformed(ActionEvent e)
try
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
XMLEncoder xe = new XMLEncoder(os);
xe.setPersistenceDelegate(DefaultTableModel.class, new DefaultTableModelPersistenceDelegate());
xe.writeObject(model);
xe.close();
Reader r = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
textArea.read(r, null);
catch (IOException ex)
ex.printStackTrace();
));
p.add(new JButton(new AbstractAction("XMLDecoder")
@Override
public void actionPerformed(ActionEvent e)
try
InputStream is = new BufferedInputStream( new FileInputStream( file ));
XMLDecoder xd = new XMLDecoder(is);
model = (DefaultTableModel)xd.readObject();
table.setModel(model);
catch (IOException ex)
ex.printStackTrace();
));
p.add(new JButton(new AbstractAction("clear")
@Override
public void actionPerformed(ActionEvent e)
model = new DefaultTableModel();
table.setModel(model);
));
JPanel pnl = new JPanel(new BorderLayout());
pnl.add(sp);
pnl.add(p, BorderLayout.SOUTH);
return pnl;
public static void main(String[] args)
EventQueue.invokeLater(new Runnable()
@Override public void run()
createAndShowGUI();
);
public static void createAndShowGUI()
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new DefaultTableModelPersistenceDelegateTest().makeUI());
f.setSize(420, 340);
f.setLocationRelativeTo(null);
f.setVisible(true);
// See following link for more information on Using XMLEncoder:
// http://www.oracle.com/technetwork/java/persistence4-140124.html
class DefaultTableModelPersistenceDelegate extends DefaultPersistenceDelegate
// Initially creates an empty DefaultTableModel. The columns are created
// and finally each row of data is added to the model.
@Override
protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder encoder)
DefaultTableModel model= (DefaultTableModel)oldInstance;
// Create XML to restore the column names
Vector<String> columnNames = new Vector<String>(model.getColumnCount());
for (int i = 0; i < model.getColumnCount(); i++)
columnNames.add( model.getColumnName(i) );
Object[] columnNamesData = new Object[] columnNames ;
encoder.writeStatement(new Statement(oldInstance, "setColumnIdentifiers", columnNamesData));
// Create XML to restore row data
Vector row = model.getDataVector();
for (int i = 0; i < model.getRowCount(); i++)
Object[] rowData = new Object[] row.get(i) ;
encoder.writeStatement(new Statement(oldInstance, "addRow", rowData));
【讨论】:
以上是关于将 Interactive JTable 的内容保存到 .txt 文件以在下次运行时读取它的主要内容,如果未能解决你的问题,请参考以下文章
如何将 JTable 的内容保存到文件中,然后在需要时将它们加载回来? [关闭]