I am trying to serialize a matrix of buttons (new JButton [6][6];) so that I can save values to a file and later reload them to the matrix. I found code online that saves the data successfully, but I am having trouble loading the data and returning its value to the matrix.
I tried using the following code:
public class SaveListener implements ActionListener {
public void actionPerformed(ActionEvent ab) {
saveArray("customlevel", buttons);
}
public void saveArray(String filename, JButton[][] write) {
try {
FileOutputStream fos = new FileOutputStream(filename);
GZIPOutputStream gzos = new GZIPOutputStream(fos);
ObjectOutputStream out = new ObjectOutputStream(gzos);
out.writeObject(write);
out.flush();
out.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
public class LoadListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
loadArray("customlevel", buttons);
}
}
public JButton[][] loadArray(String filename, JButton[][] read) {
try {
FileInputStream fis = new FileInputStream(filename);
GZIPInputStream gzis = new GZIPInputStream(fis);
ObjectInputStream in = new ObjectInputStream(gzis);
// in.readObject(read);
JButton[][] load = (JButton[][]) in.readObject();
in.close();
return load;
} catch (Exception e) {
System.out.println(e);
}
return null;
}
System.out.println(e);writee.printStacktrace();and check your logs again.