gpt4 book ai didi

java - 如何将数据加载到Jtable中

转载 作者:行者123 更新时间:2023-12-01 11:09:51 33 4
gpt4 key购买 nike

我一直在做一个小程序来测试java中jtable的保存和加载数据。保存没有问题,但是无法加载数据。当我加载数据表时显示为空,其中没有任何内容。我可以有什么建议吗!这是代码:

       public class NewOne extends JFrame {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
NewOne frame = new NewOne();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}

public NewOne() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JButton Button = new JButton("save");
Button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveTable();
}
});
Button.setBounds(10, 11, 89, 23);
contentPane.add(Button);

JButton Button1 = new JButton("load");
Button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loadTable();
}
});
Button1.setBounds(150, 11, 89, 23);
contentPane.add(Button1);

table = new JTable();
table.setModel(new DefaultTableModel(
new Object[][] {
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
{null, null, null, null, null},
},
new String[] {
"New column", "New column", "New column", "New column", "New column"
}
));
table.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
table.setBounds(20, 45, 372, 80);
contentPane.add(table);
}

private JFileChooser myJFileChooser = new JFileChooser(new File("."));

private DefaultTableModel tableModel = new DefaultTableModel();
private JTable myJTable = new JTable(tableModel);
private JTable table;

private void saveTable() {
if (myJFileChooser.showSaveDialog(this) ==
JFileChooser.APPROVE_OPTION ) {
saveTable(myJFileChooser.getSelectedFile());
}
}

private void saveTable(File file) {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(file));
out.writeObject(tableModel.getDataVector());
out.writeObject(getColumnNames());
out.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}

private Vector<String> getColumnNames() {
Vector<String> columnNames = new Vector<String>();
for (int i = 0; i < myJTable.getColumnCount(); i++)
columnNames.add(myJTable.getColumnName(i) );
return columnNames;
}

private void loadTable() {
if (myJFileChooser.showOpenDialog(this) ==
JFileChooser.APPROVE_OPTION )
loadTable(myJFileChooser.getSelectedFile());
}

private void loadTable(File file) {
try {
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(file));
Vector rowData = (Vector)in.readObject();
Vector columnNames = (Vector)in.readObject();
tableModel.setDataVector(rowData, columnNames);
in.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}

最佳答案

我猜你的问题出在这里:

 private void saveTable(File file) {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(file));
out.writeObject(tableModel.getDataVector());
out.writeObject(getColumnNames());
out.close();
}

如果我没有读错你的代码,你的tableModel是一个new DefaultTableModel();。根据文档:“DefaultTableModel()构造一个默认的 DefaultTableModel,它是一个零列零行的表。”因此,您将一个空文件放入 OutputSream 中。

也许您只是使用了错误的表格,毕竟您想获取 myJTable 的内容吗?如果这是真的,解决方案应该如下所示:

private void saveTable(File file) {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(file));
out.writeObject(myJTable.getDataVector());
out.writeObject(getColumnNames());
out.close();
}

如果我错了,请纠正我。看国外代码好难啊:P

关于java - 如何将数据加载到Jtable中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32498271/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com