user2228462
user2228462

Reputation: 619

Making a Button Stay Clicked

How do I make it so a button stay clicks after the user has exited the program?

Let's say I have this option(as a JButton) that sets the JTextArea's text to red. The user clicks the button, then exits the program. When the user opens the program back up, the text is red - meaning the button is still clicked.

If you don't get that example, let me show you another one:

I have a JButton, and a JLabel. When the button is clicked, it changes the background of the JLabel to blue. The user clicks the button, then exits the program. When the user opens the program back up, the button is already clicked, meaning the JLabel's background color is blue.

Is there a way I can do that?

Edit: I would prefer if I could do it without loading external files.

Edit 2: I like the idea of using Preference. But, could you give me an examplre for me for one of the examples above? I'm kind of a beginner in Java, so I have a lot of questions. Like, does the code go in the actionPerformed of the button? And, how could I save different kinds of information(JTextarea foreground as red) with Preferences?

Upvotes: 0

Views: 187

Answers (3)

Eng.Fouad
Eng.Fouad

Reputation: 117589

Edit 2: I like the idea of using Preference. But, could you give me an examplre for me for one of the examples above? I'm kind of a beginner in Java, so I have a lot of questions. Like, does the code go in the actionPerformed of the button? And, how could I save different kinds of information(JTextarea foreground as red) with Preferences?

Do it like this:

private static Preferences prefs = Preferences.userNodeForPackage(className.class);
private JFrame frame;
private JTextArea textArea;

public void init()
{
    frame = new JFrame();
    textArea = new JTextArea();

    // ...

    String storedValue = prefs.get("textAreaColor", null);
    if(storedValue != null)
    {
        Color color = new Color(Integer.parseInt(storedValue));
        textArea.setForeground(color);
    }

    frame.addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosed(WindowEvent e)
        {
            Color color = textArea.getForeground();
            int rgb = color.getRGB();
            prefs.put("textAreaColor", Integer.toString(rgb));
        }
    });

    // ...
}

Upvotes: 2

Daniel Kaplan
Daniel Kaplan

Reputation: 67320

You should use Preferences for this. That explanation is a little complex. But basically it saves values that can be retrieved between runs.

Here's a tutorial of how to use this with a JFileChooser. Here's an example I wrote:

package com.sandbox;

import java.util.prefs.Preferences;

public class Sandbox {

    public static void main(String[] args) {
        Preferences prefs = Preferences.userNodeForPackage(Sandbox.class);
        Integer counter = Integer.valueOf(prefs.get("counter", "0"));
        System.out.println(counter);
        counter++;
        prefs.put("counter", String.valueOf(counter));
    }
}

The first time you run this, it'll print "0". The next time you run it, it'll print "1".

Upvotes: 4

Kninnug
Kninnug

Reputation: 8053

Save the settings to an external file. Then (try to) read that file when the program starts and apply the settings.

Upvotes: 1

Related Questions