11: Simple Text Editor
Fall 2006, David Matuszek
Write a text editor.
Your editor should be a Swing GUI application. The GUI will consist of a window
with a single scrollable
JTextArea filling the entire window,
and a single
File menu. When you start the program, the window
should have a reasonable initial size.
File menu will have the following menu items:
Save As..., and either
choice). Choosing one of these menu items does all the usual things. However,
since you may not have thought about what all the "usual things" are,
here is a detailed description of what each menu item does.
Note 1: The ellipsis (
...) after a menu command means that the command will ask for more information before it is completed, usually in a
Note 2: A file is considered to be "dirty" if it has been edited but not saved--that is, if the version in memory is different from the version on disk. Your program should use a
boolean dirtyvariable to keep track of this.
JFileChooserto allow the user to choose a file. If successful, the contents of the new file will be displayed for editing, and the location of the file is remembered; but if an exception occurs, the editor displays a dialog telling the user about the error.
Load...command) with the edited contents and, if successful, sets the dirty flag to
false. However, if the location is not known (this will happen if the user opens the editor and just starts typing, without first loading a file), this menu choice actually performs a
Save As.... If any exceptions occur, use a dialog to inform the user.
JFileChooserto allow the user to select a location in which to save the file. If a file already exists at that location, uses a dialog box to ask the user if the existing file should be replaced. If any exceptions occur, use a dialog to inform the user. If the operation was completed successfully, the chosen location should be saved for use by any subsequent
Savecommands, and the dirty flag should be set to
The actual editing is done in the
JTextArea. This component already
supports basic editing capabilities; you don't need to add any features to
Any change to
the display text should set the
dirty flag to
to accomplish this, see
To add scroll bars (as needed) to a JTextArea, instead of
JScrollPane myJScrollPane = new JScrollPane(myJTextArea);
For just about everything else, execute my SwingExamples.jar, or refer to the Java API.
Note: To prevent possible file corruption (if something happens in the middle of saving the file), the correct way to save a file is to save to a uniquely named temporary file, then replace the original file with the temporary file. You don't have to do this, but I wanted you to know about it.
This assignment really requires only one class; please call it
The GUI is very simple, so you should be able to get that set up (and the
instance variables declared) in lab today. Since I would like everyone to
get at least a little bit of experience with file I/O and exception handling,
one of you should handle the load command and the other should handle the
Please turn in, via Blackboard, an executable jar file (named
by midnight Sunday, December 10. The jar file should contain your source
code as well as your class files.