0

This is how I add all of my JButtons to the panel and to the arraylist

  private ArrayList<JButton> b;
  String defaultLogo = "O";


  for(int i=0; i<81;i++)
  {

      b.add(new JButton(defaultLogo));
      b.get(i).addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent e) {
            for (int i = 0; i < b.size(); i++){
                if (e.getSource() == b.get(i)){
                    b.get(i).setText(getSymbol());
                    b.get(i).setForeground(getColor());
                    b.get(i).setBackground(getBackColor());
                }
            }

        }

      });
      tilePanel.add(b.get(i));

  }

The program allows a user to choose a symbol, background color, and foreground color and when each JButton is pressed it changes to the selected symbol, foreground color, and background color.

I want to be able to save the JButton configuration using DataOutputStream and DataInputStream. I have two action listeners attached to a save and load button that activate a save and load method when pressed. What should I write in each method to allow a user to save and load files of the JButton configurations.

save = new JMenuItem("Save");
  file.add(save);
  save.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==save){
            save();
        }

    }

  });

  load = new JMenuItem("Load");
  file.add(load);
  load.addActionListener(new ActionListener(){

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == load){
            load();
        }

    }

    });

here's an image of the program when its running

And another one

  • [Using XMLEncoder](http://www.oracle.com/technetwork/java/persistence4-140124.html) and/or [JAXB](https://docs.oracle.com/javase/tutorial/jaxb/intro/) – MadProgrammer Apr 30 '17 at 22:30

1 Answers1

0

You can try serialization, saving an object that holds your buttons.

Here's an example:

The object that holds the data

import java.io.Serializable;
import java.util.ArrayList;

public class Config implements Serializable {
    private static final long serialVersionUID = 1L;
    private ArrayList<String[]> Data;
    public Config(){
        Data=new ArrayList<String>();

    }

    public void addData(String path){
        Data.add(path);
    }
    public String getData(int index){
        return Data.get(index);
    }

}

For serialization:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Paths;
import java.io.File;
public class FileSerial {

    private FileSerial(){}

    public static Config deserialize(){

        Config result;

        try {
            FileInputStream fis = new FileInputStream(Paths.get("path to your file"));
            ObjectInputStream ois = new ObjectInputStream(fis);
            result = (Config) ois.readObject();
            ois.close();
        } catch (Exception e){
            e.printStackTrace();
    }

        return result;

    }

    public static void serialize(Config obj){
        try {

            File file = new File(Paths.get("path to file"));
            if(!file.exists()){
                file.getParentFile().mkdirs();
            }

            FileOutputStream fos = new FileOutputStream(path.toString());
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
            oos.close();

        } catch (Exception e){
            e.printStackTrace();
        }
    }

}

The filename of the object to serialize should have the extension ".ser"

Luis
  • 14
  • 1
  • 1
  • [*"Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder."*](https://docs.oracle.com/javase/8/docs/api/javax/swing/JButton.html) – MadProgrammer Apr 30 '17 at 22:29
  • would it be possible to save them as .til ? – Calvin Stineburg Apr 30 '17 at 23:17
  • @MadProgrammer How would I implement this in my man class so that when save and load methods are activated the configurations are saved/loaded? – Calvin Stineburg May 01 '17 at 00:00
  • @CalvinStineburg I left to links as a comment to your original question, I suggest having a look at them and seeing which approach suits your needs - JAXB is more flexible, but might take more time to setup – MadProgrammer May 01 '17 at 00:03