Mithun's Memoirs

« JavaFX Look and Feel | Main | DTrace performance »
Saturday Dec 08, 2007

Pure JavaFX Demo

I got an e.mail from one Mr. Tim Goeke for my previous post. I was trying to demonstrate the scripting capabilities that could be built into your existing Java application with JavaFX and build great looking user interfaces. However, if you had observed it carefully, only a part of the source listing used the scripting facility. I was still importing classes from the mainstream Java API. This made me think a bit and I decided to demonstrate pure JavaFX scripting here. The steps to be followed are the same as before. I shall post the source listing here with the GUI output.

/*
 * TestJavaFX.fx
 *
 * Created on 08.12.2007, 17:19:45
 */

package testfx;

/**
 * @author Mithun Sridharan
 */

import javafx.ui.*;
//Create a class which holds the count of the button clicks      
    class TestJavaFX {
//Declare the field: numClicks as a Number
        attribute numClicks: Number;
    }
//Create an object of the TestJavaFX class
    var demoObject = new TestJavaFX();
//Create a frame that contains the rest of the components
    Frame {
//Declare the frame parameters
        width: 200
        height:200
        resizable:false
//Configure the layout of the components          
        content: GridPanel {
            border: EmptyBorder {
                top: 10
                left: 10
                bottom: 10
                right: 10
                }
                rows: 2
                columns: 1
                vgap: 10
//Create a cell with 2 rows and 1 column and place a button and a label there;
//when you click the button, the numClicks is incremented and displayed in the label              
                cells:
                [Button {
                    width:75
                    height:25
                     text: "Click"
                     mnemonic: I
                     action: operation() {
                         demoObject.numClicks++;
                    }
                },
                Label {
                    text: bind "Number of button clicks: {demoObject.numClicks}"
                }]
            }
            visible: true
        };

The source is pretty straight forward. When you compile and execute this program, you see a nice GUI like this:

On clicking the button, the numClicks was incremented and displayed as you could see from above. You could also see that the window cannot be resized. This is because I set the resizable value to false. You could also observe how the components are nested. The frame contains a Grid, which contains a button and a label. This way, the components are hierarchically grouped and placed.

You could also see that the look and feel is that of Windows, where I tried this program. I'm not sure at the moment whether JavaFX guesses the L&F of the host platform, but I need to find that out.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed