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 3: Refactoring Swing JPad's advanced UI features

Migrate Swing JPad's dialogs, clipboard, and drag-and-drop to JavaFX

  • Print
  • Feedback

Page 4 of 6

  1. The setPrefWidth(60.0) call on each button sets the button's preferred width to 60.0. This ensures that both buttons have the same width, which improves the dialog box's aesthetics.
  2. The javafx.scene.layout.HBoxBuilder class is used to create a javax.scene.layout.HBox instance that horizontally lays out the Yes and No buttons. This container is then added to the VBox instance returned from VBoxBuilder.build() to ensure a nice-looking UI.
  3. When Swing shows a modal dialog box, the event-dispatch thread doesn't execute subsequent code until the dialog box is dismissed. Although JavaFX follows this behavior where FileChooser is concerned (FileChooser delegates to the underlying platform's native file chooser), it permits the JavaFX application thread to continue executing code that follows a call to show a modal stage. We have to proceed with caution when handling the modal dialog box, lest we face unintended consequences.

Tip: Handling modal dialogs

If a user attempted to close the primary stage immediately after showing a modal child stage then the child stage would never be displayed, because the JavaFX thread would execute the close code immediately after executing the code that shows the modal child stage. To address this behavior, we can introduce a pair of fields and methods into AreYouSure that let the application assign separate event handlers to the Yes and No buttons. When the user presses Yes, the Yes button's event handler executes. Similarly, the No button's event handler executes when the user presses No. This way, no UI code need follow the call to show the AreYouSure dialog box. The appropriate code will execute via the handler assigned to Yes or No, as shown in Listing 6.

Listing 6. Registering separate event handlers for AreYouSure

EventHandler<ActionEvent> ehae;
ehae = new EventHandler<ActionEvent>()
{
   @Override
   public void handle(ActionEvent ae)
   {
      if (fDirty)
      {
         AreYouSure ays = new AreYouSure(stage, SAVE_CHNGS);
         EventHandler<ActionEvent> ehae1;
         ehae1 = new EventHandler<ActionEvent>()
         {
            @Override
            public void handle(ActionEvent ae)
            {
               if (doSave())
                  doNew();
            }
         };
         ays.setOnYes(ehae1);
         EventHandler<ActionEvent> ehae2;
         ehae2 = new EventHandler<ActionEvent>()
         {
            @Override
            public void handle(ActionEvent ae)
            {
               doNew();
            }
         };
         ays.setOnNo(ehae2);
         ays.show();
         // There should be no UI code here.
      }
      else
         doNew();
   }
};
miNew.setOnAction(ehae);

Figure 3 shows the AreYouSure dialog box.

Figure 3. Click Yes if you're sure, No if you're not, or X to cancel

The system clipboard

JPadFX supports system clipboard access via cut, copy, and paste operations. Much of this support is handled automatically by TextArea, which accesses the system clipboard and performs a cut, copy, or paste operation when it detects a specific key combination, such as Ctrl+C.

TextArea also inherits methods from its javafx.scene.control.TextInputControl superclass to programmatically access the system clipboard; void copy() is one example. Action-event handlers for JPadFX Edit menu items Cut, Copy, and Paste execute appropriate methods (such as ta.copy();) when an item is selected.

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