October 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Using NetBeans to Develop a JavaFX Desktop Application

  • July 1, 2009
  • By Anghel Leonard
  • Send Email »
  • More Articles »

Develop the Main Class of the Application

The main class of your application is named Main.fx, and it is available in the Projects view, under the DeltaCars -> Source Packages -> deltacars node. As you can see, NetBeans already has populated it with the main imports and it has defined a default Stage and a Scene for your application. These JavaFX classes, much like CustomNode, Group, and Node, represent the main instruments of the JavaFX language. Here is a brief overview of the classes:
  • Stage: This class represents the top-level container for the JavaFX application.
  • Scene: This class represents a canvas on which JavaFX renders graphical content. It usually contains Nodes that represent graphical components. The Scene is contained by Stage.
  • CustomNode: This class represents a subclass of Node. It is created by overriding the create function. It can have one child node.
  • Group: This class represents a set of child nodes.
  • Node: This class represents a graphical component on a scene.

Now, if you examine the Main.fx generated code, you will notice that by default NetBeans uses a Stage of 250x80 pixels, titled "Application title" and a Scene that contains a single Node of type Text. To modify these settings for your application, follow these steps:

  1. Rename the Stage title by replacing "Application title" with "Delta Cars."
  2. Resize the Stage replacing the 250x80 pixels with 712x442 pixels.
  3. Because you need a window that is not resizable, add the resizable: false attribute.
  4. Delete all the content on the Scene, leaving only the corresponding accolades.
  5. Set the Scene background color to black: Add the Scene's fill attribute, expand the Colors Palette, drag the Black item from the palette, and drop it where the fill's value should be written (you can use the Colors Palette wherever you need to set a color property). Notice that a Color is an instance of the javafx.scene.paint.Color class.
  6. Your Scene is now empty. So the content attribute should look like content: [].

At this point, the code of Main.fx should look like this:

package deltacars;
 
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
 
Stage {
    title: "Delta Cars"
    width: 712
    height: 442
    resizable: false
    
    scene: Scene {
        fill:Color.BLACK
        content: []
    }
}

If you run the project, you should see an empty black window titled "Delta Cars."

Author's Note: If you ever want to add a Stage or a Scene from scratch, you can expand the Applications Palette and drag-and-drop the corresponding item in the JavaFX class editor. This will provide a basic implementation populated with the minimum requirements.


Tags: JavaFX



Page 2 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel