0

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;
}
7
  • 2
    And what's the specific problem ? Exception etc.? Commented Feb 21, 2013 at 15:28
  • Problem is in loading value back to Buttons array, it doesn't work although code seems to be alright and doesn't have any exception problems. Commented Feb 21, 2013 at 15:30
  • 3
    what does "doesn't work" mean? we going to play 20 questions? Commented Feb 21, 2013 at 15:30
  • 1
    instead of System.out.println(e); write e.printStacktrace(); and check your logs again. Commented Feb 21, 2013 at 15:33
  • 1
    @ArslanBerbic The code you posted is really messy - can you please format it a bit and fix the indentation? I tried to format it through Eclipse, but at least the braces do not match at all Commented Feb 21, 2013 at 15:34

2 Answers 2

2

In loadArray() you are both passing in an array and returning an array. the passed in array is ignored. are you somehow expecting that the passed in array will be populated?

Sign up to request clarification or add additional context in comments.

Comments

0

I think the confusion is coming from the fact that you are returning loaded array from your loadArray() method, but ignoring the result of the function call in your ActionListener.performAction function. If you want buttons to have the return value, just assign it, so do

buttons = loadArray(fileName)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.