Building a Java text editor
JToolBar
and populated it with three JButton
components that already display icons. All you have left to do is specify the text for each button's label and tool tip, and create an actionPerformed()
event for each button from which you'll call the appropriate event-handling method.
jButton1
in the component tree, then click the Properties tab in the Inspector.
toolTipText
property to highlight its entry. Type Open File,
if it doesn't already say that, and press Enter.
jButton2
and jButton3
, using the following text:
Save File
for jButton2
.
About
for jButton3
.
Many controls define a "default" event in their BeanInfo
class. For example, a button defines actionPerformed()
as its default event. To quickly generate an event handler for the default event, double-click the control in the UI designer.
Using this shortcut, create events for the buttons as follows:
jButton1
in the UI designer. This should switch you to the editor and place your cursor inside the new jButton1_actionPerformed(ActionEvent e)
event for the Open button.
fileOpen()
method:
//Handle toolbar Open button fileOpen();
jButton2_actionPerformed(ActionEvent e)
event for jButton2
and call saveFile()
from it:
//Handle toolbar Save button saveFile();
jButton3_actionPerformed(ActionEvent e)
event for jButton3
, and call helpAbout()
from it:
//Handle toolbar About button helpAbout();
Notice that the code in the jButton1
and jButton3
event-handlers make calls to methods which don't exist yet: fileOpen()
and helpAbout()
. Let's create those methods now.
The purpose of the fileOpen()
method will be to perform the operations that are currently in your File|Open menu handling method. However, since you need to perform the same operations when the Open button is pressed, you'll create the fileOpen()
method so you can have just one copy of that code, and call it from both the File|Open menu and the Open button.
You can use the following steps to create the method:
fileOpen()
method stub. You can put this method just above the openFile()
method. The stub should look like this:
// Handle the File|Open menu or button, invoking okToAbandon and openFile // as needed. void fileOpen() { }
jMenuItem2_actionPerformed()
. The code selected should be:
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();
fileOpen()
method stub.
Here is what the completed fileOpen()
method should look like:
// Handle the File|Open menu or button, invoking okToAbandon and openFile // as needed. void fileOpen() { 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(); }
fileOpen()
from the File|Open event hander, which you should modify to look like this:
void jMenuItem2_actionPerformed(ActionEvent e) { // Handle the File|Open menu item. fileOpen(); }
helpAbout()
method and call it from both the menu and button event handlers.
Use the following steps to do this:
helpAbout()
method just before the fileOpen()
method:
// Display the About box. void helpAbout() { }
jMenuHelpAbout_actionPerformed()
into the new helpAbout()
method stub:
TextEditFrame_AboutBox dlg = new TextEditFrame_AboutBox(this); Dimension dlgSize = dlg.getPreferredSize(); Dimension frmSize = getSize(); Point loc = getLocation(); dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y); dlg.setModal(true); dlg.show();
helpAbout();
into jMenuHelpAbout_actionPerformed()
so the method looks like this:
//Help | About action performed public void jMenuHelpAbout_actionPerformed(ActionEvent e) { helpAbout(); }