Reputation: 125
I have been doing a small program to test save and load data by jtable in java. there is no problem in saving but can't load the data. when I load the data table shows empty, nothing in there. May I have any suggestions! here is the Code:
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();
}
}
}
Upvotes: 0
Views: 2457
Reputation: 3042
I guess your problem lies here:
private void saveTable(File file) {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(file));
out.writeObject(tableModel.getDataVector());
out.writeObject(getColumnNames());
out.close();
}
Your tableModel
is a new DefaultTableModel();
if i haven't missread your code. per documentation : "DefaultTableModel()
Constructs a default DefaultTableModel which is a table of zero columns and zero rows."
therefore you put an empty file to your OutputSream.
Maybe you just used he wrong table after all do you want to get the content of your myJTable
? If that is true the solution should look like:
private void saveTable(File file) {
try {
ObjectOutputStream out = new ObjectOutputStream(
new FileOutputStream(file));
out.writeObject(myJTable.getDataVector());
out.writeObject(getColumnNames());
out.close();
}
Correct me if I am wrong. Its difficult to look at foreign code :P
Upvotes: 1