Exercise 223 Modify a text editor In this exercise youll add
Exercise 22-3 Modify a text editor
In this exercise, you’ll add code to the beginnings of a basic, but functional, text editor. When you are finished with this exercise, your application should look like this:
Review the application
1. Open the project named ch22_ex3_TextEditor.
2. Review the code in the MainWindow class. Note that the doOpenButton method uses a JFileChooser component. This method displays a Swing dialog that allows the user to select a file from the file system. Then, it prints the contents of the selected file to the console.
3. Run the application to see how it works. It should display a frame that doesn’t have any components on it.
Add buttons to the frame
4. In the buildButtonPanel method, add an Open button and a Save button to the panel. Then, add tooltips to the buttons.
5. Add action listeners to the Open button and Save button that invoke the doOpenButton and doSaveButton methods.
6. In the constructor for the frame, add the button panel to the north area of the BorderLayout for the frame. To do that, you can call the buildButtonPanel method to get the panel for the buttons.
7. Run the application to make sure it works correctly. When you use the Open button to open a text file on your system, it should print the contents of the file to the console. When you use the Save button to save a file, it should print a message to the console that indicates that the file has been saved.
Add a JTextArea control to the frame
8. In the MainWindow class, add an instance variable for a JTextArea component named textArea. Although the JTextArea component isn’t covered in this book, it works similarly to the JTextField component, except that it allows multiple lines of text.
9. In the constructor, create a new instance of the JTextArea and add it to the center area of the BorderLayout for the frame.
10. In the doOpenButton method, find the line of code that prints the contents of the file to the console. Replace this line with code that sets the text of the JTextArea component to the contents of the file.
11. In the doSaveButton method, add a line of code that sets the fileContents variable to the text that’s in the JTextArea component.
12. Run the application to make sure it works correctly. You should be able to open a text file, edit it, and save those changes. However, if you open a text file that’s too large to fit on the screen, it will disappear off the bottom of the application.
Add a JScrollPane control to the frame
13. To fix this problem, add the JTextArea to a JScrollPane and then add the JScrollPane to the center area of the BorderLayout for the frame.
14. Run the application to make sure it works correctly. Now, if you open a large text file, scrollbar should appear that allow you to scroll through the file.
------------------------------------------------------------------------------------------------------------
package murach.texteditor;
public class Main {
public static void main(String[] args) {
MainWindow mainWin = new MainWindow();
}
}
------------------------------------------------------------------------------------------------------------
package murach.texteditor;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class MainWindow extends JFrame {
private File file;
private String fileContents = \"\";
public MainWindow() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException |
IllegalAccessException | UnsupportedLookAndFeelException e) {
System.out.println(e);
}
setTitle(\"Text Editor\");
setSize(800, 600);
setLocationByPlatform(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// TODO: Add the button pannel to the JFrame.
setVisible(true);
}
private JPanel buildButtonPanel() {
JPanel panel = new JPanel();
// TODO: Implement the rest of this method.
return panel;
}
private void doOpenButton() {
JFileChooser openDialog = new JFileChooser();
int choice = openDialog.showOpenDialog(this);
if (choice == JFileChooser.APPROVE_OPTION) {
file = openDialog.getSelectedFile();
fileContents = \"\";
try {
fileContents = new String(
Files.readAllBytes(Paths.get(file.toURI())));
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,
\"The file could not be read.\", \"File read error\",
JOptionPane.ERROR_MESSAGE);
}
// TODO: display file contents in JTextArea control instead of on console
System.out.println(fileContents);
}
}
private void doSaveButton() {
if (file != null) {
// TODO: get file contents from JTextArea control instead of on console
try {
Files.write(Paths.get(file.toURI()), fileContents.getBytes());
System.out.println(\"The file was saved!\");
} catch (IOException ex) {
JOptionPane.showMessageDialog(this,
\"The file could not be written.\", \"File write error\",
JOptionPane.ERROR_MESSAGE);
}
// TODO: display file contents in JTextArea control instead of on console
System.out.println(fileContents);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------
Solution
//display contents in jtextarea
//todo get contents from Jtextarea
//todo display file contents in JTextarea instead of console


