|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 5 of 6
A document event is fired when the user makes a change to the text-area component's document. This component's registered document listener
responds by setting fDirty (which is initially false) to true, as shown in Listing 4.
DocumentListener dl;
dl = new DocumentListener()
{
@Override
public void changedUpdate(DocumentEvent de)
{
// Not needed -- only called when attributes are changed
}
@Override
public void insertUpdate(DocumentEvent de)
{
fDirty = true;
}
@Override
public void removeUpdate(DocumentEvent de)
{
fDirty = true;
}
};
ta.getDocument().addDocumentListener(dl);
The document listener declares insertUpdate() and removeUpdate() methods that are called whenever content is inserted into or removed from the Document object.
A menu event is fired when the user selects a menu. The menu's registered menu listener responds by updating the status bar with menu-specific help text. When the user unselects the menu, the status bar's content is reset to default text. Listing 5 demonstrates both tasks.
MenuListener ml;
ml = new MenuListener()
{
@Override
public void menuCanceled(MenuEvent me)
{
}
@Override
public void menuDeselected(MenuEvent me)
{
lbl.setText(DEFAULT_STATUS);
}
@Override
public void menuSelected(MenuEvent me)
{
lbl.setText("New doc|Open existing doc|Save changes|Exit");
}
};
mFile.addMenuListener(ml);
The menu listener's menuCanceled() method is left empty because this method is never called. It appears to be a historical artifact.
A caret event is fired when the user moves the caret (text-insertion indicator) within the text area component. The component's registered caret listener determines whether or not text is selected and enables or disables the Cut and Copy menu items appropriately, as demonstrated in Listing 6.
CaretListener cl;
cl = new CaretListener()
{
@Override
public void caretUpdate(CaretEvent ce)
{
if (ta.getSelectedText() != null)
{
miCut.setEnabled(true);
miCopy.setEnabled(true);
}
else
{
miCut.setEnabled(false);
miCopy.setEnabled(false);
}
}
};
ta.addCaretListener(cl);
The caret listener's caretUpdate() method calls JTextArea's String getSelectedText() method to determine whether text has been selected, then updates Cut and Copy accordingly.
Finally, we come to the window event, which is fired whenever the user attempts to close the window or perform another window operation. The frame window's registered
window listener invokes doExit(), as shown in Listing 7. (This listener requires setDefaultCloseOperation() to be called with DO_NOTHING_ON_CLOSE as its argument; otherwise, the window listener will be ignored.)
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter()
{
@Override
public void windowClosing(WindowEvent we)
{
doExit();
}
});
doExit() prompts the user to save changes when fDirty is true. Whether or not changes are saved, it lastly invokes dispose() to destroy the frame window and terminate the application. Listing 8 shows this code sequence.
More from JavaWorld