JavaFX rocks
Today, I downloaded NetBeans 6.0 and played with it for a while. I was looking at the plugins and found that there was this JavaFX plugin that was available. I've been musing about this JavaFX stuff for a while now and decided to give it a go. Its really a breeze to use JavaFX for developing applications with a nice user interface. First and foremost, I fired up my NetBeans 6.0 IDE and installed the JavaFX plugin. Once the plugin was installed, NB went for a restart and I found the IDE ready for JavaFX stuff. I shall not go deep into how to install the plugin and such stuffs. They are described well at the NB homepage.
Once the plugin was installed, I created a JavaFX project. The screenshots are self explanatory.
Once the class was created, I hacked on to create the source as follows:
/*
* HalloWorld.fx
*
* Created on 07.12.2007, 16:46:33
*/
package testfx;
/**
* @author Mithun Sridharan
*/
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.lang.System;
var frame = new JFrame();
//Create a frame
var n = 0;
//Declare a variable n and initialize it to zero
var button = new JButton("Press me"
;
//Create a button
frame.getContentPane().add(button);
//Add the button to the frame
button.addActionListener(new ActionListener() {
operation actionPerformed(event) {
System.out.println(n);
n++;
}
});
// Add an action listener to the button and when the button is clicked, then count it upwards and display it in the console
frame.pack();
frame.setVisible(true);
The source is pretty simple and straightforward. The first four lines are import statements and here, we are importing the classes from Swing. However, note that the rest of the lines are pretty similar to a scripting language. These lines are the JavaFX script commands which instruct, what and how the components should be built and displayed. On compiling the above source listing, there were two outputs : one on the console and the other a GUI, which is the following frame.
This is a simple program. Now, its interesting to see how functions could be written and the program modularized. To check this out, I extended the program to include a function which calculates the square of the number from 0 onwards. The changed listing is shown below:
/*
* HalloWorld.fx
*
* Created on 07.12.2007, 16:46:33
*/
package testfx;
/**
* @author Mithun Sridharan
*/
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.lang.System;
import java.lang.Math;
var frame = new JFrame();
var n = 0;
var button = new JButton("Press me"
;
frame.getContentPane().add(button);
button.addActionListener(new ActionListener() {
operation actionPerformed(event) {
System.out.println(square(n));
n++;
}
});
frame.pack();
frame.setVisible(true);
function square(n){
return Math.pow(n,2);
}
The source listing is pretty similar, other than the fact that we have now included the function square(n). Note that we haven't used the standard typo, which could be function square(int n). This is indeed amazing. Look at the output:
To summarize, JavaFX is pretty straight forward, easy to use and simple. You could use it to design great user interfaces. More on this topic would follow as I venture out. Keep checking often.
Posted at 05:28PM Dec 07, 2007 by mithun in JavaFX | Comments[0]