Last lab, gang. Here goes.
Today you guys will make a little text editor
Here's some detail of the menu:
You guys will need to make a Lab10.java file that does this.
You will need to make 1 (one!) class: a Frame called Lab10. It will derive from the JFrame class and implement the ActionListener interface. All told, it will have 2 properties and 3 methods.
Surely you guys know how to write this one by now.
Oh, okay... Make a new instance of the Lab10 class and call its show() method.
Set up the frame: by calling setTitle(), setDefaultCloseOperation() and setSize().
Add the text area: by adding it to a JScrollPane and then putting the scroll pane in the center of the frame. (Look at this JScrollPane constructor which shows how to set the policy for displaying the scrollbars.)
Create the menu: Look at the Lab8 notes for a refresher on how to do this. In a nutshell, you:
Create the toolbar: This process is very similar to the menu:
For the button images, feel free to use these:
new.gif | open.gif | save.gif | exit.png |
Hints on the menu items and toolbar buttons: You can make the code more compact by using a couple String arrays that contain label names and image file names and then use a for loop to create the menu items and buttons and add them to their containers. You can then re-use the labels as the action command strings. See UsesActionCommands.java for an example. Also, see this entry in the Q&A section below.
You inherit this method from the ActionListener interface. This is where the real meat of your program is.
"Save" event: Do the following:
Note#1: You will need to handle all of the various exceptions that could be thrown by the file I/O methods. Consult the Ch 11-12 notes for details.
Note#2: You should set up your code such that the user only needs to see the save dialog once. The first time he clicks "Save", the save dialog should be displayed. Thereafter, it should just use the previously chosen save file name and re-save it.
Note#3: You do not have to bother with setting up a file filter for the file chooser dialog, just let it display all files. (You're welcome.)
"Open" event: The code you will write for this event handler is very similar to the "Save" event:
"New" event: clear the text area, reset the save file to null, and set the frame's title to "[No File]".
"Exit" event: by calling System.exit(0) (you don't have to prompt the user to save before closing; this is not a user friendly app)
Not telling. Find 'em yourself.
No, that is tedious and difficult. Moreover, not all text files end with .txt (such as HTML and other docs). Just let it display the default of "all files".
When you get the name of the file the user has chosen, do not type:
saveFile = chooser.getSelectedFile().getName();Instead, type:
saveFile = chooser.getSelectedFile().getCannonicalPath();which will retrieve the full path to where the file is stored.
I will take pity on you this time and offer you the following source code snippet. It should go inside your frame's constructor.
// set up menu & toolbar String[] labels = {"New", "Open", "Save", "Exit"}; String[] tbFiles = {"new.gif", "open.gif", "save.gif", "exit.png"}; String[] tooltips = {"Create a new file", "Open an existing file", "Save the current file", "Close the application"}; JMenu fileMenu = new JMenu("File"); JToolBar toolbar = new JToolBar(); for (int i = 0; i < labels.length; i++) { // menu item setup JMenuItem mi = new JMenuItem(labels[i]); mi.setActionCommand(labels[i]); mi.addActionListener(this); mi.setToolTipText(tooltips[i]); fileMenu.add(mi); // toolbar button setup JButton btn = new JButton(new ImageIcon(tbFiles[i])); btn.setActionCommand(labels[i]); btn.addActionListener(this); btn.setToolTipText(tooltips[i]); toolbar.add(btn); } // add menubar to frame JMenuBar mb = new JMenuBar(); mb.add(fileMenu); setJMenuBar(mb); // add toolbar to frame getContentPane().add(toolbar, BorderLayout.NORTH);
Yeah, I know.
Only if you want credit.
Those of you that are doing a text editor will find this lab a good start. I would expect you to add a few more features though, like: search and replace, checking to see if the buffer has changed before exit / new are clicked; support for cut / copy / paste; and possibly support for undo / redo. (Not to mention a cute little 'About' dialog.)