board

Sunday, March 18, 2012

Hello World in JavaFX 2.0


Before talking about the example itself, I want to show you how to create a JavaFX application in NetBeans. (If you haven´t installed JavaFX and NetBeans yet, please see my previous post Installing JavaFX 2.0 and NetBeans 7.7.1) Click on “New Project” in the “File” menu to open the project wizard. Then choose “JavaFX->JavaFX Application” and press “Next”.




In the next dialog you can specify the name of your application and a destination folder, where it should be stored. If you have installed JavaFX correctly the “JavaFX Platform” should be specified already. Otherwise you can add the platform yourself by clicking on “Manage Platforms->Add Platform” and specifying the paths to your JavaFX installation.




Note: By default the “Create Application Class” checkbox is checked. Please uncheck it because we´ll create our own application class.
Click on “finish” to create your first JavaFX application.



Hello World in JavaFX 2.0 – Example 1

Probably every single software developer knows the famous „HelloWorld“ example as it is often used to show the syntax of a (unknown) programming language and to give a first clue, of what the language looks like. I don´t want to break this tradition, so here are 2 different versions of a HelloWorld program in JavaFX 2.0. I´ll show the complete code first and then explain the individual parts.

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

/**
 * 
 * Created on: 17.03.2012
 * @author Sebastian Damm
 */
public class HelloJavaFX extends Application
{
    @Override
    public void start(Stage stage) throws Exception
    {        
        Button bt = new Button("Print HelloWorld");
        bt.setOnAction(new EventHandler<ActionEvent>() 
        {
            @Override
            public void handle(ActionEvent arg0)
            {
                System.out.println("HelloWorld! :)");
            }
        });
        
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 150);
        root.getChildren().add(bt);

        stage.setTitle("HelloWorld in JavaFX 2.0");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args)
    {
        Application.launch(args);
    }
}


The first thing worth mentioning is that you have to extend from the Application class in order to create a working JavaFX application. This class provides several live-cycle methods and is the starting point for your application. It is an abstract class (which means, that you cannot instantiate it) with a single abstract method start, that you have to override. You are provided a stage object by the JavaFX runtime, which you can use to display your UI.

Saturday, March 17, 2012

Starting a series of JavaFX 2.0 beginner tutorials

As mentioned in my 2 introduction posts, I´ll start a series of beginner tutorials for JavaFX 2.0.
I will start with the obligatory “HelloWorld” example and try to cover as many as possible beginner topics in the next few weeks. Feel free to tell me in the comments, what you want to see.

Who are the tutorials targeted at?

  • People with average to good Java knowledge
  • People that what to learn JavaFX 2.0 but have little to no knowledge yet

I won´t cover any Java basics, but we will start at the very beginning of JavaFX 2.0


Friday, March 16, 2012

Installing JavaFX 2.0 and NetBeans 7.7.1


My first blog post will cover the most essential part => Installing JavaFX 2.0 and setting up your development environment.
As of Java SE 7 Update 2, Java SE includes JavaFX 2.0, so if you don´t have a version of Java installed, go for this package. If you already have installed a different Java SE/EE version you can also download JavaFX 2.0 stand-alone.

Here is the download of the latest Java SE version, which is 7 Update 3:

If you want to install Java EE as well, you can download the package with JavaSE and JavaEE.
Note: It is "only" JavaSE 7 Update1, which means, that JavaFX 2.0 isn´t included. If you download this package, you´ll have to download the JavaFX 2.0 SDK separately.

The standalone version of JavaFX 2.0 can be found here:

Either way, simply download the appropriate file for your operating system and follow the installation instructions.

Thursday, March 15, 2012

Welcome to my blog

So, welcome to my blog. First of all I would like to introduce myself quickly: My name is Sebastian Damm, I am 23 years old, I live in Germany and I am about to finish my apprenticeship as a software developer. As English isn´t my native language, please excuse any spelling or grammar mistakes. Nevertheless I want to write my blog in English to make it appeal to a broader audience.


What is this blog all about?

As I am a big Java fan, the majority of posts will be about different Java technologies and news. Because I learned JavaFX 2.0 in the last few weeks, I decided, that it´ll be a good idea to post some beginner tutorials and introductions about JavaFX 2.0, because many people want to learn it at the moment. So expect the first several posts to cover some JavaFX 2.0 basics and beginner examples.