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 2: Refactoring Swing JPad's basic UI features to JavaFX

Migrate JPad's content pane, menu system, and event handling to JavaFX

  • Print
  • Feedback

Page 5 of 6

The former case is handled by registering an event handler with Menu's onShowing property via the void setOnShowing(EventHandler<Event> value) setter. Similarly, the latter case is handled by registering an event handler with Menu's onHiding property via the void setOnHiding(EventHandler<Event> value) setter. All of this is shown in Listing 6.

Listing 6. The File menu's help text is customized for its environment

EventHandler<Event> ehe;
ehe = new EventHandler<Event>()
{
   @Override
   public void handle(Event e)
   {
      if (getHostServices().getWebContext() == null)
         lbl.setText("New doc|Open existing doc|Save changes|Exit");
      else
         lbl.setText("New doc|Open existing doc|Save changes");
   }
};
mFile.setOnShowing(ehe);
ehe = new EventHandler<Event>()
{
   @Override
   public void handle(Event e)
   {
      lbl.setText(DEFAULT_STATUS);
   }
};
mFile.setOnHiding(ehe);

The first handle() method determines the environment in which JPadFX is running before displaying the help text. If JPadFX is running within a web page, Exit is not included on the status bar because the File menu does not have an Exit menu item in this context: it does not make sense to exit JPadFX in a web context.

JPadFX learns about its host environment by calling Application's HostServices getHostServices() method. This method returns a javafx.application.HostServices instance, whose methods let JavaFX learn about and interact with the environment. For example, a call to netscape.javascript.JSObject getWebContext() would return the JavaScript handle of the enclosing Document Object Model window of the web page containing your application. If the application isn't embedded in a web page, the return value will be null.

Window events

The javafx.stage.WindowEvent class describes events related to showing, hiding, or attempting to close a window. JPadFX registers with the primary stage an event handler that responds to a close request by calling Window's void setOnCloseRequest(EventHandler<WindowEvent> value) method, as shown in Listing 7.

Listing 7. doExit(Event) consumes window events

EventHandler<WindowEvent> ehwe;
ehwe = new EventHandler<WindowEvent>()
{
   @Override
   public void handle(WindowEvent we)
   {
      doExit(we);
   }
};
primaryStage.setOnCloseRequest(ehwe);
// ...
private void doExit(Event e)
{
   // ...
   e.consume();
}

The handle() method calls doExit(Event), which factors out common event handling code: the close request can originate from the Exit menu item (when this item is present) or from closing the application via the title bar. doExit(Event) invokes Event's void consume() method to prevent the event from propagating to the JavaFX runtime, which would close the window without giving the user an opportunity to save changes.

Change listeners

Although not technically events, change listeners are similar to them. Recall that Swing JPad detects document changes by registering a document listener with the text-area component's document. In contrast, JPadFX registers a change listener with the text-area control's length property (see below), assigning true to fDirty whenever the length changes.

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