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

Before ImageView is instantiated, Image is instantiated and told to load contents of image.png. Because this PNG file will be stored in a JAR file, it's accessed via the expression getClass().getResourceAsStream("icon.png").

Next, the ImageView node's width and height are obtained to help position the scene. This node is positioned 10 pixels from the left edge of the dialog box and centered vertically. It is then added to the group's observable list. (See Part 2 for a discussion about observable lists.)

The javafx.scene.text package's Text and Font classes are instantiated to describe two text nodes that are displayed with the image. The text is colored white to contrast with the scene's black background. The font is assigned and these nodes are added to the group's observable list.

JavaFX lets you add one or more effects to a node. For example, javafx.scene.effect.Reflection is used to reflect a node. About's constructor instantiates Reflection and calls its void setFraction(double value) method, specifying that all of the image must be visible. The constructor also calls javafx.scene.Node's void setEffect(Effect effect) method on the group node to reflect this node's contents.

The scene is now created and an event handler is registered to respond to mouse-pressed events. When the mouse is pressed over the About stage, Stage's void close() method will be called to close the stage.

Tip: Centering the stage

After assigning the scene to the stage, there's just one thing left to do. Centering the About stage over the primary stage will add a touch of professionalism to our UI. We do this by calling Stage's setX() and setY() property setter methods with the results of our centering calculations, as shown in the final two lines of Listing 3.

Figure 1 shows the About dialog box.

Figure 1. Click anywhere on the About dialog box to close it

The Alert dialog

The Alert dialog box, which displays messages resulting from I/O exceptions, is implemented by the Alert class. Listing 4 reveals Alert's source code.

Listing 4. The Alert class declares only a constructor

public class Alert extends Stage
{
   public Alert(Stage owner, String msg)
   {
      setTitle("Alert");
      initOwner(owner);
      initStyle(StageStyle.UTILITY);
      initModality(Modality.APPLICATION_MODAL);
      Button btnOk = new Button("Ok");
      btnOk.setOnAction(new EventHandler<ActionEvent>()
                        {
                           @Override
                           public void handle(ActionEvent ae)
                           {
                              close();
                           }
                        });
      final Scene scene = new Scene(VBoxBuilder.create()
                                               .children(new Label(msg),
                                                         btnOk)
                                               .alignment(Pos.CENTER)
                                               .padding(new Insets(10))
                                               .spacing(20.0)
                                               .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);
   }
}

Alert's constructor takes a Stage argument identifying the stage that owns the Alert dialog box. It also takes a String argument that describes the message to be shown. JPadFX passes the primary stage instance and a suitable message to this constructor.

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