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 3 of 6

For example, when an I/O error occurs, JPadFX executes new Alert(stage, "I/O error: "+ioe.getMessage()).show(); to notify the user via a dialog box. The primary stage instance is passed as the first argument; an exception message is passed as the second argument.

The constructor creates a scene consisting of the message and an OK button (whose event handler closes the stage). The javafx.scene.layout.VBoxBuilder class creates a single-column scene consisting of a label containing the message and a button. The scene is centered in the dialog box.

VBoxBuilder is an example of a builder class that lets you create a container by conveniently chaining method calls together. create() instantiates this class. Remaining method calls specify the builder's children, alignment, padding, and spacing. The final build() call creates and returns a javafx.scene.layout.VBox instance, which is a layout container that lays out its children in a single vertical column.

Alert() uses the Scene(Parent root) constructor to specify the scene graph's root node. A width and height are not specified because we want the scene's size to be automatically calculated based on the preferred size of its content.

Window's void sizeToScene() method is called to set the stage window's height/width to match the scene's height/width. Stage's void setResizable(boolean value) method is called with a false argument to prevent the stage from being resized.

Tip: Getting the scene's width and height

The centering calculations for the Alert dialog box rely on scene.getWidth() and scene.getHeight(), which return the scene's width and height. But these method calls return 0.0, because explicit width and height values were not specified for the scene. To address this problem, we have to briefly show and then hide the dialog box and its scene, thus prompting scene.getWidth() and scene.getHeight() to return the property values.

Figure 2 shows the Alert dialog box.

Figure 2. Click OK or X on the title bar to close the Alert dialog box

AreYouSure?

The AreYouSure dialog box, which display a message and prompts the user to press a Yes or No button, is implemented by the AreYouSure class. Listing 5 reveals AreYouSure's source code.

Listing 5. The AreYouSure class declares a private field, a constructor, and a pair of methods

public class AreYouSure extends Stage
{
   private EventHandler<ActionEvent> ehaeYes, ehaeNo;
   public AreYouSure(Stage owner, String msg)
   {
      setTitle("Are You Sure?");
      initOwner(owner);
      initStyle(StageStyle.UTILITY);
      initModality(Modality.APPLICATION_MODAL);
      Button btnYes = new Button("Yes");
      btnYes.setOnAction(new EventHandler<ActionEvent>()
                         {
                            @Override
                            public void handle(ActionEvent ae)
                            {
                               if (ehaeYes != null)
                                  ehaeYes.handle(ae);
                               close();
                            }
                         });
      Button btnNo = new Button("No");
      btnNo.setOnAction(new EventHandler<ActionEvent>()
                        {
                           @Override
                           public void handle(ActionEvent ae)
                           {
                              if (ehaeNo != null)
                                 ehaeNo.handle(ae);
                              close();
                           }
                        });
      btnNo.setPrefWidth(60.0);
      btnYes.setPrefWidth(60.0);
      HBoxBuilder hbb;
      hbb = HBoxBuilder.create()
                       .children(btnYes, btnNo)
                       .spacing(10);
      VBoxBuilder vbb;
      vbb = VBoxBuilder.create()
                       .children(new Label(msg), hbb.build())
                       .padding(new Insets(10.0))
                       .spacing(10.0)
                       .alignment(Pos.CENTER);
      Scene scene = new Scene(vbb.build());
      setScene(scene);
      sizeToScene();
      setResizable(false);
      show(); hide(); // needed to get proper value from scene.getWidth() and
                      // scene.getHeight()
      setX(owner.getX()+Math.abs(owner.getWidth()-scene.getWidth())/2.0);
      setY(owner.getY()+Math.abs(owner.getHeight()-scene.getHeight())/2.0);
   }
   public void setOnYes(EventHandler<ActionEvent> ehae)
   {
      ehaeYes = ehae;
   }
   public void setOnNo(EventHandler<ActionEvent> ehae)
   {
      ehaeNo = ehae;
   }
}

AreYouSure's constructor creates a message box that displays a message centered over a pair of Yes and No buttons. The constructor is largely similar to Alert's constructor, but with three essential differences:

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