Building a Java text editor
To do this, create a new method that will update the title bar, then call it from places where the code changes either the current file name or the dirty
flag. Name this new method updateCaption()
.
jMenuFileExit_actionPerformed(ActionEvent e)
method in the structure pane. This moves the cursor to that event handling method and highlights it in the editor. Click in the editor to place the cursor just above this method and insert the following updateCaption()
method:
// Update the title bar of the application to show the filename and its dirty state. void updateCaption() { String caption; if (currFileName == null) { // synthesize the "Untitled" name if no name yet. caption = "Untitled"; } else { caption = currFileName; } // add a "*" in the caption if the file is dirty. if (dirty) { caption = "* " + caption; } caption = "Text Editor - " + caption; this.setTitle(caption); }
updateCaption()
from each of the places the dirty
flag actually changes or whenever you change the currFileName
.
Specifically, put the call updateCaption();
in the following places:
try
block of the TextEditFrame()
constructor, as the next line immediately after the call to jbInit()
.
//Construct the frame public TextEditFrame() { enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); updateCaption(); } catch(Exception e) { e.printStackTrace(); } }
try
block of the openFile()
method.
try { // Open a file of the given name. File file = new File(fileName); // Get the size of the opened file. int size = (int)file.length(); // Set to zero a counter for counting the number of // characters that have been read from the file. int chars_read = 0; // Create an input reader based on the file, so we can read its data. // FileReader handles international character encoding conversions. FileReader in = new FileReader(file); // Create a character array of the size of the file, // to use as a data buffer, into which we will read // the text data. char[] data = new char[size]; // Read all available characters into the buffer. while(in.ready()) { // Increment the count for each character read, // and accumulate them in the data buffer. chars_read += in.read(data, chars_read, size - chars_read); } in.close(); // Create a temporary string containing the data, // and set the string into the JTextArea. jTextArea1.setText(new String(data, 0, chars_read)); // Cache the currently opened filename for use at save time... this.currFileName = fileName; // ...and mark the edit session as being clean this.dirty = false; // Display the name of the opened directory+file in the statusBar. statusBar.setText("Opened "+fileName); updateCaption(); } catch (IOException e) { statusBar.setText("Error opening "+fileName); }
this.dirty=false
in the try
block of saveFile()
.
try { // Open a file of the current name. File file = new File (currFileName); // Create an output writer that will write to that file. // FileWriter handles international characters encoding conversions. FileWriter out = new FileWriter(file); String text = jTextArea1.getText(); out.write(text); out.close(); this.dirty = false; updateCaption(); return true; } catch (IOException e) { statusBar.setText("Error saving "+currFileName); } return false;
if
block of the File|New menu handler jMenuItem1_actionPerformed()
.
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; updateCaption(); } }
dirty
flag is first set in a clean file due to user typing. This is done in each of the document1
event handlers which should be changed to read:
void document1_changedUpdate(DocumentEvent e) { if (!dirty) { dirty = true; updateCaption(); } } void document1_insertUpdate(DocumentEvent e) { if (!dirty) { dirty = true; updateCaption(); } } void document1_removeUpdate(DocumentEvent e) { if (!dirty) { dirty = true; updateCaption(); } }
Congratulations! You now have a functional text editor written in JBuilder!
For additional suggestions on improving this tutorial, send email to jpgpubs@inprise.com.
JBuilder Professional and Enterprise users: Proceed to Step 16 to deploy this application and run it from the command line.