Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

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

Practical JavaFX 2, Part 1: Architecture of a Swing-based notepad

How will Swing JPad's UI features map to JavaFX 2.0?

  • Print
  • Feedback

Page 5 of 6

Document events

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.

Listing 4. changedUpdate() is not applicable to document changes

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.

Menu events

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.

Listing 5. menuSelected() is called each time a menu is selected

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.

Caret events

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.

Listing 6. caretUpdate() is called each time the caret moves

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.

Window events

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.)

Listing 7. doExit() closes the frame window

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.

  • Print
  • Feedback

Resources

More from JavaWorld

  • Find more of Jeff's writing in Java Tutor, his blog on JavaWorld.
  • See the JavaWorld Site Map for a complete listing of research centers focused on client-side, enterprise, and core Java development tools and topics.
  • JavaWorld's Java Technology Insider is a podcast series that lets you learn from Java technology experts on your way to work.