Building a Java text editor
boolean
variable called dirty
.
jMenuItem1_actionPerformed(ActionEvent e)
dirty
and currFileName
variables. Place it immediately after the line jTextArea1.setText("");
and before the closing curly brace.
// clear the current filename and set the file as clean: currFileName = null; dirty = false;
You'll use the JOptionPane
dialog to display a confirmation message box to find out from the user whether to save a dirty file before abandoning it when doing a File|Open, File|New, or File|Exit. This dialog is invoked by calling a class method on JOptionPane
, so you do not need to add a JOptionPane
component to your program.
okToAbandon()
method to the source code. You can put this new method right after the saveAsFile()
method:
// Check if file is dirty. // If so get user to make a "Save? yes/no/cancel" decision. boolean okToAbandon() { int value = JOptionPane.showConfirmDialog(this, "Save changes?", "Text Edit", JOptionPane.YES_NO_CANCEL_OPTION) ; switch (value) { case JOptionPane.YES_OPTION: // yes, please save changes return saveFile(); case JOptionPane.NO_OPTION: // no, abandon edits // i.e. return true without saving return true; case JOptionPane.CANCEL_OPTION: default: // cancel return false; } }The above method, which you'll finish later, will be called whenever the user chooses File|New, File|Open, or File|Exit. Its purpose is to test to see if the text needs to be saved (is "dirty"). If it is dirty, this method uses a Yes, No, Cancel Message dialog for asking the user whether to save.
This method also calls saveFile()
if the user clicks the Yes button. When the method returns, the <CODE>boo lean</CODE> return value, if true, indicates it is OK to abandon this file because it was clean or the user clicked the Yes or No button. If the return value is false, it means the user clicked Cancel. The code that will actually check to see if the file has changed will be added in a later step.
For now, this method always treats the file as dirty, even if no change has been made to the text. Later you will add a method to set the dirty
variable to true when the user types in the text area, and you will add code to the top of okToAbandon()
to test the dirty
variable.
okToAbandon()
method at the top of your File|New and File|Open event handlers, as well as in the wizard-generated File|Exit event handler. In each case, test the value returned by okToAbandon()
and only perform the operation if the value returned is 'true'.
Tip: To find these event handlers quickly, click them in the structure pane. You can also search in the structure pane by moving focus to the pane and typing.
The following are the modified event handlers:
okToAbandon()
returns true. The modified method should now look like this:
void jMenuItem1_actionPerformed(ActionEvent e) { // Handle the File|New menu item. if (okToAbandon()) { // clears the text of the TextArea jTextArea1.setText(""); // clear the current filename and set the file as clean: currFileName = null; dirty = false; } }
okToAbandon()
returns false. The modified method should now look like this:
void jMenuItem2_actionPerformed(ActionEvent e) { //Handle the File|Open menu item. if (!okToAbandon()) { return; } // Use the OPEN version of the dialog, test return for Approve/Cancel if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) { // Call openFile to attempt to load the text from file into TextArea openFile(jFileChooser1.getSelectedFile().getPath()); } this.repaint(); }
okToAbandon()
around the line of code that exits the application. The modified method should now look like this:
//File | Exit action performed public void jMenuFileExit_actionPerformed(ActionEvent e) { if (okToAbandon()) { System.exit(0); } }
Each of these menu event handling methods now only does its task if okToAbandon()
returns true.
okToAbandon()
isn't completed yet (it will be finished in a later step). Right now, it always acts like the file is dirty. The result is that for now the confirmation message box always comes up when you choose File|New, File|Open, or File|Exit, even if the text hasn't been changed.