Edijae Crusar
Edijae Crusar

Reputation: 3541

Saving changes made in a JFrame

I have a simple JFrame where the user is changing the background color of it using a JPopupMenu. When the user exit the application, I want to save the changes made in the background.

I have tried to handle this with method windowClosing() from WindowAdapter class but when when I launch the application again, I don't see the changes I made earlier. I don't know what the problem is. I will appreciate any help. Here's my code.

/*i have removed unnnecessary codes*/

public class Popupframe extends JFrame{
private JRadioButtonMenuItem[] items;
private final Color[] colorvalues={Color.BLUE,Color.YELLOW,Color.RED};
static Color bgcolor=Color.CYAN;
JRadioButtonMenuItem[] cheek;

public Popupframe() {
    super("using popups");
    String[] colors = {"Blue","Yellow","Red"};

    setBackground(bgcolor);
    addMouseListener(new Handler());
}

private class Handler extends MouseAdapter implements ActionListener {


    @Override
    public void actionPerformed(ActionEvent event) {
        for(int i=0; i<items.length; i++) {
           if(event.getSource()==items[i]) {
               getContentPane().setBackground(colorvalues[i]);
               bgcolor=colorvalues[i];
           }
       }    
    }
}

public static void main(String[] args) {
    Popupframe frame=new Popupframe();
    frame.setSize(width,height);
    frame.setDefaultCloseOperation(Popupframe.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
            int ok=JOptionPane.showConfirmDialog(frame,"are sure?","close",JOptionPane.WARNING_MESSAGE);
            if(ok==JOptionPane.OK_OPTION) {
                bgcolor=frame.getContentPane().getBackground();
                System.out.println(bgcolor);
                System.exit(0);
            }
        }
    });
    frame.setVisible(true);
}

Upvotes: 0

Views: 616

Answers (2)

Oliver Watkins
Oliver Watkins

Reputation: 13519

You are not persisting the color. Color is serializable, so you could just save the object in the program root directory. Put this code in your WindowClosing method :

//serialize the Color
try (
  OutputStream file = new FileOutputStream("myBgColor.ser");
  OutputStream buffer = new BufferedOutputStream(file);
  ObjectOutput output = new ObjectOutputStream(buffer);
){
  output.writeObject(bgColor);
}  
catch(IOException ex){
  log.log(Level.SEVERE, "Cannot perform output.", ex);
}

When you reload the application you need to get the color back. In the PopupFrame() constructor, before you call setBackground(color), put in this code here :

//deserialize the Color file
try(
  InputStream file = new FileInputStream("myBgColor.ser");
  InputStream buffer = new BufferedInputStream(file);
  ObjectInput input = new ObjectInputStream (buffer);
){
  //deserialize the List
  bgColor = (Color)input.readObject();
}
catch(ClassNotFoundException ex){
  fLogger.log(Level.SEVERE, "Cannot perform input. Class not found.", ex);
}

That should do the trick.

Upvotes: 1

Abin
Abin

Reputation: 550

You need to save the color code into a file(like settings file or shared preference) before System.exit(0) and read it in the main and set that color code. Then it works fine.

Upvotes: 2

Related Questions