JavaFX First Tutorial
In this section we will make a simple Hello World application. As it is gui so we will use gui elements to display the text. The first example will be done using command line tools. Make sure that you have Java5+ version installed in your machine, otherwise see Installing Java.Let's write a script file using JavaFX script. This is similar to writing the program in Java language.
Let's write the script file which will display the Hello World. Name the script file as "HelloWorld.fx". Open the script file in any text editor and write the following
HelloWorld.fx
//Import is similar to Java language imports import javafx.stage.Stage; //Stage represents the top level container and will contain other things. Stage { title: "Hello World" width: 400 height:400 }
Compile the file using javafx compile. Make sure the command line tools for JavaFx are in the path. The tools are in the bin folder of JavaFX installed directory.
javafxc HelloWorld.fx
Once the compilation happens successfully, there will be a HelloWorld.class file created in the directory. This is similar to normal Java program.
To run the application, use javafx similar to java command.
javafx HelloWorld
It will open an empty widnow with title "HelloWorld".
Now let's write a text on the window using other constructs
import javafx.stage.Stage; //imports for JavaFX import javafx.scene.Scene; import javafx.scene.text.Font; import javafx.scene.text.Text; //Stage represents the top level container and will contain other things. Stage { title: "Hello World" width: 400 height:400 //Definition of scene scene: Scene { content: [ Text { content: "Hello World" x:10 y:10 font: Font { name: "courier" size: 12 } } ] } }
You can download the file from the attachment down below.
In the above code Scene contains all the content. In this case it has a Text element which is "Hello World". The content should be painted starting at x and y values with font courier and size 12. How the output will be will depend on the media.
Again run the javafxc and javafx tool. This time the window rendered will have Hello World on it.
JavaFX is a declarative language in the sense that we tell about the things that we want and leave it to the environment to handle it. The runtime decides as per the media how to handle the output.
Under the hood the JavaFX compiler generates a normal Java class file. If the generated HelloWorld.class file is decompiled, it will be a normal Java file
public class HelloWorld extends com.sun.javafx.runtime.FXBase implements com.sun.javafx.runtime.FXObject{
JavaFX framework shields the developer by exposing the gui constructs through simple declarative JavaFX script.
Running the Code in Eclipse
Make sure you have plugin installed in eclipse as per JavaFX Installation. Make a JavaFX project using Files->New->Other->JavaFX Project wizard. Note that the project structure is similar to a Java project. The difference is the JavaFX libraries being included in the build path.
Make a new JavaFX script file in src folder. Name the file as HelloWorld.fx. Copy the HelloWorld.fx content as in the above example. Run the application as JavaFX application. (Click on Run->run and choose JavaFX application).
Next JavaFX Index Java Home Home
Sidebar
Last wiki comments
- AOP: Thanks
- Lalit Bhatt: Superb Collection
- Lalit Bhatt: J2EE training
- Introduction to ORM: timberland shoes
- Introduction to ORM: jordan shoes
- Introduction to ORM: nike air max
- Pune Tourist Spots: KONARK PARK CLOSED
- jQuery Form Validations: Jquery Developer
- Spring Introduction: RG
- SOAP: Re: Assertion
Sidebar
Random Pages
- Comprehensive Overview of Java Technologies
- SOAP
- Pune Hill Forts For Trekking
- Windows Antivirus
- Source Code Control Systems
- JavaFX Data Binding and Trigger
- Introduction to Seam
- CSS - Handling Browser Moods
- Parsers Introduction
- Spring Annotation driven Aspects
- JAXB Validation using Schema
- Spring POJO Aspects
- What markets work on?
- Why projects fail?
- Bharat Band - Jai ho
- The concept of Nation
- Don't hide complexity if it cannot be handled in a robustway
Last blog post comments
-
Bharat Band - Jai ho: How do we protest?
Wed 18 of Aug., 2010 13:13 IST
-
Divided by Destiny: Contact
Fri 23 of July, 2010 16:02 IST
-
Future of Java: thesis writing
Sat 17 of July, 2010 01:50 IST
-
Hang till Death Mr. Kasab: some change
Mon 28 of June, 2010 16:03 IST
-
God Religion : Why we are confused?: Re: Is GOD Necessary?
Tue 15 of June, 2010 17:29 IST
-
God Religion : Why we are confused?: Is GOD Necessary?
Tue 15 of June, 2010 13:06 IST
-
The reason in religion: good
Wed 10 of Mar., 2010 18:30 IST
-
The confusion of Design Patterns: I think at macro level you are right...
Tue 23 of Feb., 2010 03:31 IST
-
The Indian Municipality: Comment
Fri 22 of Jan., 2010 13:20 IST
-
What Government should do?: Re: Review of the Indian Law and Order and Justice Dispensation regime.
Fri 22 of Jan., 2010 13:16 IST
Post new comment