作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 JTable(类型 AbstractTableModel)中编辑的行保存到文件中。我不知道该怎么做;或者可以使用 ObjectOutput 和 InputStream。
此外,我想在保存时从另一个 JTable 加载它。
为了给你一个更好的了解,我正在做一个EPOS系统,用户可以编辑行数据(数量),当程序执行时,发现数量不为零,它会保存整行(与当前详细信息)到文件中。这样用户就可以拥有已下订单的记录(针对 EPOS 和库存中的销售功能)。
当用户单击“确认”按钮时,有望触发保存部分。
有人可以帮我吗?谢谢
最佳答案
如果您阅读 JTable
API,您会发现它建议您使用 XMLEncoder
进行长期存储。
I am trying to save edited rows from a JTable (type AbstractTableModel)
您没有使用 AbstractTableModel。 AbstractTableModel不提供任何数据存储。您正在使用扩展 AbstractTableModel 的 TableModel。假设您使用的是 DefaultTableModel
,这里是一个允许您使用 XMLEncoder 和 XMLDecoder 的示例:
// Following code is a more complete version of:
// http://stackoverflow.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));
}
}
}
class DefaultTableModelPersistenceDelegate2 extends DefaultPersistenceDelegate
{
// Initially creates a DefaultTableModel with rows and columns. Then the
// columns are reset and proper names are used. Finally data is set for each
// cell in the model.
@Override
protected void initialize(Class<?> type, Object oldInstance, Object newInstance, Encoder encoder)
{
super.initialize(type, oldInstance, newInstance, 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 reset the value of every cell to its value
for (int row = 0; row < model.getRowCount(); row++)
{
for (int col = 0; col < model.getColumnCount(); col++)
{
Object[] o = new Object[] {model.getValueAt(row, col), row, col};
encoder.writeStatement(new Statement(oldInstance, "setValueAt", o));
}
}
}
}
如果您没有使用 DefaultTableModel,那么您将需要创建自己的 PersistenceDelegate。
关于java - 如何将 AbstractTableModel 保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33583219/
我是一名优秀的程序员,十分优秀!