Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

A Veteran JFC/Swing Developer Makes the Leap to JavaFX Script : Page 4

Find out why using JavaFX Script to build GUIs and add rich graphics functionality to Java applications is a no-brainer for seasoned Java Foundation Classes/Swing developers.


advertisement
The GUI Features
JavaFX Script greatly simplifies the coding needed to develop rich Internet applications. All the Swing components and containers are available in JavaFX Script, along with their default content layout managers. The runtime library javafxrt.jar contains all the Java classes that interact with underlying Java components. The implementation details are beyond the scope of this article, but avid architects could look into javafxrt.jar to find out how the factory patterns are used to instantiate the underlying JDK classes at runtime.

The GUI Components
All the AWT/Swing components are available in JavaFX Script, and except for the layout managers they carry the same naming conventions. You call JavaFX components as widgets, similar to the x-windows naming convention. Within JavaFX Script, all widgets inherit from the base Widget class. The Widget class in turn has a read-only reference to the underlying Java component class using generics.

In JavaFX Script, you could instantiate a Label component using the following code:



Label { text: "Hello World Label" }

Even though the width and height attributes of the label can be set, the underlying layout manager controls how the label is displayed.

Similarly you could initialize a Button using this code:

Button { text: "I'm a button!" mnemonic: I action: operation() { System.out.println("button clicked.."); } }

These simple lines implement the complete button functionality (refer to FrontendSample1.fx for a complete example of GUI components).

Besides the attributes defined in the above snippet, the following self-explanatory attributes also are available for the Button class:

  • Icon
  • selectedIcon
  • pressedIcon
  • rolloverIcon
  • rolloverSelectedIcon
  • rolloverEnabled
  • disabledIcon
  • disabledSelectedIcon

By default whenever you create a Button, JavaFX Script adds an action listener with a reference to the user-defined action attribute.

All JavaFX components that receive user input extend from ActionWidget, which has an attribute action registered as a function. If the action attribute is defined, it is automatically registered as a callback function. Similarly TextField and TextArea are defined and implemented.

Layout Managers
All the layout managers from Swing are available in JavaFX Script, but you refer them with a slightly different naming convention. The Layout portion of the layout manager name is replaced with Panel. For example:

  • FlowPanel corresponds to FlowLayout
  • GridPanel corresponds to GridLayout

The functionality remains the same, however. For example:

  • FlowPanel arranges the components in a flow honoring the width and height of the widgets.
  • BorderPanel provides the entire space for the widgets placed inside them without honoring the width and height of the contained widgets.
  • GridbagPanel provides all the attributes to control the layout and dynamic behavior of the components.

Figure 1 shows the use of the BorderPanel, FlowPanel, GridPanel, and GridbagPanel layouts. Each layout retains all the properties from its Java counterpart.

Click to enlarge

Figure 1. Layout Managers in Use

You also can add components to the container by placing them within brackets ([ ]) separated by commas.

What's Next?
While not much JavaFX Script documentation is available currently, you can get a head start on the language by downloading the script files inside accompanying source code. Once you get the hang of it, you may have second thoughts about ever using Java Swing again. Why would you when JavaFX Script enables you to build highly responsive Internet and desktop applications so much more easily?



Raghu Donepudi, an independent contractor, currently is a technical manager for a federal government agency. He has a master's degree in Computer Science from Lamar University in Texas. He is a Sun-certified Java developer and the author of many software design techniques.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap