Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
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
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.
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.
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.
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.
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.
More from JavaWorld