-
Saving objects problem
I'm not sure if this is the correct section, but I'll go ahead and post. I have 3 button groups, consisting of 2 JRadioButtons. I have Two JTextFields. How do I get it so after the first time the person presses the set up Button (Which brings up to all the components above), it will save what I put in the text fields, and which buttons I press? I'm just trying to get one down, I'll get the rest. In one of the text fields, you have to enter a method name(this is a utility program).
I also set up a static int so it increments itself everytime you click the apply button (closes the window).
When you click the set up button and opens the set up window:
Code:
if (setUpClick >= 1) {
try {
FileInputStream setUpInfo = new FileInputStream("Set Up Info.ser");
ObjectInputStream setUpObject = new ObjectInputStream(setUpInfo);
String theMethodName = (String) setUpObject.readObject();
setUpObject.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
methodName.setText(theMethodName);
}
When I click the apply button:
Code:
public class ApplyButton implements ActionListener {
public void actionPerformed(ActionEvent event) {
try {
FileOutputStream setUpWriting = new FileOutputStream("Set Up Info.ser");
ObjectOutputStream objectSetUp = new ObjectOutputStream(setUpWriting);
objectSetUp.writeObject(theMethodName);
objectSetUp.close();
} catch (IOException ex) {
ex.printStackTrace();
}
setUpClick++;
setUpFrame.dispose();
}
}
This doesn't work. I don't know why.
-
I'm not sure that I fully understand your question, but perhaps you don't want to dispose of your GUI window but rather call setVisible(false) on it. Then if you are to display it again, first check if it is null and if so, create the gui window, but if not, call setVisible(true) on it -- in other words do a lazy initialization of it.
-
Oh man, I never thought of doing it that way. By setting it to false that sets the frame to null correct? So setting it back to visible would have all these options still there? Sounds much easier. Thanks. Yes, you answered the question.