JavaFX successor of Swings for developing Desktop applications
JavaFX is the new User Interface ToolKit,
introduced by Oracle to develop Desktop based applications and Rich Internet
Applications. JavaFX was introduced as the successor of Swing, with the
intention to replace Swing. In 2007 Sun microsystems announced the launch of
JavaFX Script language for designing Rich Internet Applications to compete with
Adobe Flex and Microsoft Silverlight. In 2011 JavaFX Script was replaced with JavaFX
2.0 and it became the part of native Java library. From Java 8 onwards JavaFX
is being shipped with the standard Java Development Kit. Since its launch JavaFX
didn’t receive much attention in developing web and mobile based applications,
but it can be used as a better replacement of Swings for developing desktop
applications.
Some of the Key features that makes JavaFX a
better choice than Swings for developing Desktop applications:
Use of FXML: FXML is an XML
based technique of designing user interface (UI), which separates the
application logic from the user interface. An user can directly create the user
interface using XML or use a JavaFX Scene Builder. The advantage of using FXML are: that FXML is
not a compiled language, there is no need to recompile the code after changes,
it uses XML which is familiar to many developers and easy integration with
various IDE’s like NetBeans, Eclipse and IntelliJ IDEA.
Use of CSS: FXML documents come with the support of CSS
to style the user interface. The default theme that comes is Modena. CSS can be
used in same way as it is used in HTML.
The CSS simplifies the look and feel of the application. Scene Builder
includes a CSS Analyzer to manage the style sheets.
Use of Scene Builder: It is a tool
used to develop the user interface by drag and drop of controls on the content
pane. The corresponding XML code will be generated automatically. Each document
generated in Scene Builder is stored as FXML document and have a controller.
Improved event handling and support of properties,
event handling and binding: JavaFX supports the properties, which is an
extension of Java Bean concept. JavaFX properties are often used with binding,
in which two properties can be binded together and changes in one reflects the
changes in other. In JavaFX instead of using simple data types like integers,
double etc, we can use different property class like DoubleProperty,
IntegerProperty etc. For each property
we can specify the event listener, which will be notified when there is any change
in property.
JavaFX is an upcoming technology from Oracle,
which can be used to develop Desktop applications, but it will take some time
to mature and get support from developer’s community. Some of the noted
applications developed in JavaFx are DirkCuratorORCaliop, developed by
EIZO GmbH in Germany,MediPi (telehealth kit) created by HSCIC London on
Raspberry Pi, Navigaotr developed at Rob Lyden a logistics company.
SampleJavaFXML
Application
Application Class: Application class act as an entry point of
the JavaFX application. The launch() method must me called from the main()
method to start the application and the start() method
must be overridden to load the FXML document.
public class TestOne extends Application {
@Override
public void start(Stage stage) throws Exception {
FXMLLoader loader = new FXMLLoader(getClass().getResource("FXTestOne.fxml"));
Parent root = (Parent) loader.load();
FXMLDocumentController controller =
loader.getController();
Scene scene = new Scene(root);
stage.setTitle("First Application");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
In above code the FXTestOne.fxml is the FXML
file, which acts as the View of the program and contains all the controls. It
is created using the Scene builder and its code is generated in XML. Below is
the code for simple FXML file containing a Button and Label.
<AnchorPane id="AnchorPane"
prefHeight="200" prefWidth="320"
xmlns:fx="http://javafx.com/fxml/1"
fx:controller="javafxapplication20.FXMLDocumentController">
<children>
<Button layoutX="106"
layoutY="95" text="Click" onAction="#ButtonAction"
fx:id="button" />
<Label layoutX="150"
layoutY="180" minHeight="16" minWidth="69"
fx:id="label" />
</children>
</AnchorPane>
There is a Controller class for every FXML
document, which controls the functioning of the controls placed on FXML
document. Below is controller for above
FXML document.
public class FXMLDocumentController implements
Initializable {
@FXML
private Label label;
@FXML
private void ButtonAction(ActionEvent e) {
label.setText("Hello user!");
}
@Override
public void initialize(URL url, ResourceBundlerb)
{
}
}
Mr. Vijay Gupta
Assistant Professor
Deptt. of Information Technology
Deptt. of Information Technology
No comments:
Post a Comment