SO I made a Java Program and there is a check box in my code. I want the check box variable saved for next startup on the JFrame application. I want the check box "Exit Launcher on Support" variable and the "Developer Console" to remember upon startup.
Code:
import javax.swing.JFrame;
public class Settings {
/**
* @wbp.parser.entryPoint
*/
static void appSettings() {
JFrame settingsApp = new JFrame();
settingsApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
settingsApp.setAlwaysOnTop(true);
settingsApp.setResizable(false);
settingsApp.setVisible(true);
settingsApp.setTitle("Launcher Settings");
settingsApp.setSize(350, 500);
settingsApp.getContentPane().setLayout(new GridLayout(1, 0, 0, 0));
JPanel panel = new JPanel();
settingsApp.getContentPane().add(panel);
SpringLayout sl_panel = new SpringLayout();
panel.setLayout(sl_panel);
JLabel lblInfo = new JLabel("This is the settings window for the DevCity13 Launcher.");
sl_panel.putConstraint(SpringLayout.NORTH, lblInfo, 10, SpringLayout.NORTH, panel);
sl_panel.putConstraint(SpringLayout.WEST, lblInfo, 10, SpringLayout.WEST, panel);
panel.add(lblInfo);
JCheckBox chckbxDevoleperConsole = new JCheckBox("Devoleper Console");
chckbxDevoleperConsole.setToolTipText("Activate the devoleper console for error/glitch checking.");
chckbxDevoleperConsole.setEnabled(false);
sl_panel.putConstraint(SpringLayout.NORTH, chckbxDevoleperConsole, 6, SpringLayout.SOUTH, lblInfo);
sl_panel.putConstraint(SpringLayout.WEST, chckbxDevoleperConsole, 10, SpringLayout.WEST, panel);
panel.add(chckbxDevoleperConsole);
JCheckBox chckbxSupportexit = new JCheckBox("Exit Launcher under Support");
chckbxSupportexit.setToolTipText("Close the Launcher Window when Support button is pressed.");
sl_panel.putConstraint(SpringLayout.NORTH, chckbxSupportexit, 6, SpringLayout.SOUTH, chckbxDevoleperConsole);
sl_panel.putConstraint(SpringLayout.WEST, chckbxSupportexit, 10, SpringLayout.WEST, panel);
panel.add(chckbxSupportexit);
}
}
Please help...
you cannot do that without storing the value somewhere, this could be a file, or a database. The easiest would be to write to a file and then reading it back on startup.
You can use PrintWriter to write your data into a file and BufferedReader to read back. That's the easiest way to do it
boolean storedExitLauncher;
boolean storedDevConsole;
try {
File dataContiner = new File("data.dat");
BufferedReader bufferedReader = new BufferedReader(new FileReader(dataContiner));
storedExitLauncher = Boolean.parseBoolean(bufferedReader.readLine());
storedDevConsole = Boolean.parseBoolean(bufferedReader.readLine());
} catch (FileNotFoundException e) {
storedDevConsole = storedExitLauncher = false;
} catch (IOException e) {
throw new RuntimeException(e);
}
//do your stuff here
boolean exitLauncher = false; //save the checkbox data
boolean devConsole = false; //save checkbox data
try {
PrintWriter printWriter = new PrintWriter("data.dat", "UTF-8");
printWriter.println(exitLauncher);
printWriter.println(devConsole);
} catch (FileNotFoundException e) {
throw new RuntimeException("Cannot save data, it will be lost");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}