Reputation: 2920
I am attempting to use a GUI to write out to a text file. I ran into a problem while doing this, and was able to figure out that the WindowEvent is somehow terminating the program early for some reason.
Initially I had outFile.close();
after the WindowEvent
line, so the text from the JTextArea wouldn't be transferred to the text file. After switching a few lines of code around, I realized that when trying to automatically close the JFrame with the WindowEvent, none of the code afterwards executed.
How can I fix this problem?
import ...
public class TxtManager {
static Input input;
public static void overwriteCurrentFile() throws IOException {
TxtManager txt = new TxtManager();
input = new Input(txt);
}
public synchronized void sendData() throws IOException {
try {
BufferedWriter outFile = new BufferedWriter(new FileWriter(file1));
String data = input.textArea.getText();
outFile.write(data);
outFile.close();
input.frame.dispatchEvent(new WindowEvent(input.frame,WindowEvent.WINDOW_CLOSING));
// Code from this point on in the try block does not execute.
System.out.println("Finished with the write out..."); // Used for pinpointing the problem
JOptionPane.showMessageDialog(null,"Data in " + TxtManager.file1 + " has been overwritten successfully.");
TxtManager.showData = true;
TxtManager.menu2();
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,"Error: " + ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
}
class Input extends Thread {
TxtManager txt;
public JTextArea textArea;
public JFrame frame;
private JButton button;
public Input(TxtManager txt) throws IOException {
this.txt = txt;
JOptionPane.showMessageDialog(null,"Enter desired data into the GUI screen.\n" +
"Press the <Done> button once you are finished.");
frame = new JFrame("Prompt");
JPanel panel = new JPanel(new BorderLayout());
frame.add(panel);
JLabel label = new JLabel("Enter desired data.");
panel.add(label,BorderLayout.NORTH);
textArea = new JTextArea(15,80);
panel.add(textArea,BorderLayout.CENTER);
panel.add(new JScrollPane(textArea));
button = new JButton("Done");
panel.add(button,BorderLayout.SOUTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
start();
}
public void run() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
txt.sendData();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,"Error: " + ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
}
});
}
}
Upvotes: 1
Views: 370
Reputation: 1694
I believe your problem stems from this line (assuming input.frame is equivalent to the frame you create here)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
From the API
EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.
What this all means is that when you throw the WindowClosing event, the frame (rightfully) tries to close itself. This in turn calls the default close operation, which happens to be EXIT_ON_CLOSE, which calls System.exit()
right there. This ends your program without executing any more lines of code. What you're probably looking for is WindowConstants.DISPOSE_ON_CLOSE
instead of Frame.EXIT_ON_CLOSE
, which should close the window and dispose its resources without exiting your program.
Although honestly, it might make more sense to just hide your window via Frame.setVisible(false);
.That way if you wanted to reuse the frame in the future, there wouldn't be as much overhead starting it up.
Upvotes: 4
Reputation: 2607
Don't close window during method execution. If you want to hide the window, use setVisible(false)
instead. Close it after finishing your method
Upvotes: 0