Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Writing text to a file.

I've tried:

connPort = port.getText();
serverIP = server.getText();
configF.setVisible(false);
try
{
BufferedWriter w = new BufferedWriter(new FileWriter(new File("sys/conf.txt")));
w.write(server.getText().concat("\n"));
w.write(port.getText());
w.write("This works!");
configL.setText("Save successful.");
}
catch(Throwable t)
{
configL.setText("Save unsucessful.");
}

Where server and port are JTextAreas holding the server and port, and configL is the label for the window. The label's set to "Save Successful.", but the file is blank after this runs. Can someone please tell me why?
EDIT 2: Above problem has been fixed. w.close(); Heh.

EDIT: configF is the JFrame holding all of this, and it should dissappear when I call configF.setVisible(false); but it doesn't. How should I go about making it dissappear?
[1180 byte] By [freeone3000] at [2007-11-11 7:06:04]
# 1 Re: Writing text to a file.
Could you post more code? setVisible(false) should work! Try adding a call to dispose().
Norm at 2007-11-11 22:39:26 >
# 2 Re: Writing text to a file.
public JPanel createConfigPane()
{
final JPanel configPane = new JPanel(new GridBagLayout());
final JTextField server = new JTextField(serverIP);
final JTextField port = new JTextField(connPort);

server.setPreferredSize(nameSize);
port.setPreferredSize(nameSize);

JButton save = new JButton("Save");
JButton cancel = new JButton("Cancel");
save.setPreferredSize(new Dimension(70, 30));
cancel.setPreferredSize(new Dimension(90, 30));

JLabel portL = new JLabel("Port:");
JLabel serverL = new JLabel("Server address:");
final JLabel configL = new JLabel("Hiya!");
final GameGUI g = new GameGUI(new JFrame());

save.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
connPort = port.getText();
serverIP = server.getText();
configF.setVisible(false);
try
{
BufferedWriter w = new BufferedWriter(new FileWriter(new File("sys/conf.dat")));
w.write(server.getText().concat("\n"));
w.write(port.getText());
configL.setText("Save successful.");
w.close();
}
catch(Throwable t)
{
configL.setText("Save unsucessful.");
}
}
});
cancel.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
configF.setVisible(false);
configL.setText("...Ehm, bye?");
configF.dispose();
}
});
//GridBagLayout junk.

return configPane;
}

I hit "Cancel", and I see "..Ehm, bye?" up on top, where the JLabel is, after I click the "Cancel" button.
freeone3000 at 2007-11-11 22:40:26 >
# 3 Re: Writing text to a file.
What is your question now?

Yes the button's actionlistener puts that text into the configL component.
Norm at 2007-11-11 22:41:30 >
# 4 Re: Writing text to a file.
Well, yes. But that's AFTER I dispose of the frame, and AFTER I set it to be invisible.
It should be set, but I shouldn't be able to see it.

Question would be: "How do I make JFrames go away?"
freeone3000 at 2007-11-11 22:42:29 >
# 5 Re: Writing text to a file.
Cant' explain it. If what you've shown is being execture, setVisible(false) should make close the frame.
I can only guess that the code isn't executing as you think it is.
Add some println()s to see where it is executing and when.
Norm at 2007-11-11 22:43:28 >
# 6 Re: Writing text to a file.
configF.setVisible(false);
System.out.println("It should be invisible.");
configL.setText("...Ehm, bye?");
configF.dispose();
System.out.println("It should be gone.");

It is printing out those two bolded lines, but the frame's still visible.
freeone3000 at 2007-11-11 22:44:27 >
# 7 Re: Writing text to a file.
Only looking at a small part of the program, its hard to say.

Does configF refer to the ONLY frame? Are there any others?
Norm at 2007-11-11 22:45:25 >
# 8 Re: Writing text to a file.
There are a few other frames, else I would just call System.exit(0);. Generic "frame", which contains the main application frame. It's the only one "there" right now. The other frames are loginF, creditF, helpF, and charF, which are the login screen, credit screen, help screen, and characater creation screen, all uninitialized at this point.

createConfigPane() is called from a generic ActionListener set to a JButton, config, in the main "frame".
freeone3000 at 2007-11-11 22:46:28 >