How-To's > How do I work with controls?


You can create controls in the following ways:

Use Built-In UI Controls

The JavaFX UI controls are built by using nodes in the scene graph, and therefore they can take full advantage of the visually rich features of the JavaFX platform and be portable across profiles. The following screen capture shows the possible uses of UI Controls in your application.

JavaFX UI Controls

All the classes that construct UI controls reside in the javafx.scene.control package:

There are a number of preview controls available in the com.javafx.preview.control package including Menu Bar, Tool Bar, Menu, Popup Menu,Tree View. See What's New in JavaFX 1.3 for more information.

Examples of Built-In Controls

Integrate Swing Components

You can add controls by integrating Swing components into your JavaFX application. See Insider's Guide to Mixing Swing and JavaFX for more information. This blog describes how Swing components can be embedded in a JavaFX scene graph.

Create Custom Controls Programmatically

You can create custom controls by using either of the following approaches:

  • Extend the CustomNode or Control classes.
  • Apply CSS styles and provide a custom skin.

The following code fragment illustrates the first approach by creating a custom UI control with an image.

class MyControl extends CustomNode {
    var button: Node[];
    var image: Image;
    override function create() {
        HBox {
            spacing: 5
            nodeVPos: VPos.CENTER
            content: bind [button, ImageView {image: image}]
        }
    }
}

 

This control can be used in your application to create radio buttons with icons.

Custom controls: radio button with icons

Look at the code fragment that implements one of them.

MyControl {
    button: RadioButton {toggleGroup: toggleGroup value: "English"}
    image: Image {url: "{__DIR__}eng.jpg"}
}

 

Alternatively, you can use MyControl to create buttons with images.

Custom controls: buttons with images

The MyControl class in this application is instantiated as follows.

MyControl {
    button: Button {text: "Accept"}
    image: Image {url: "{__DIR__}ok.png"}
},
MyControl {
    button: Button {text: "Decline"}
    image: Image {url: "{__DIR__}not.png"}

 

For another example of creating controls by extending the CustomNode class, see the following link:

  • Sudoku with CSS
    All of the UI components in this sample, including the buttons, are formatted with CSS. One button enables the user to toggle between two different color themes. In this sample, the buttons are created by extending the customNode class, not by using the built-in UI controls.

For an example of using skins to customize the look of UI controls, see the following link:

  • Style Editor
    This sample demonstrates how to use CSS to reskin controls to create a new look for different sizes of view screens, branding, or just plain fun.

API Documentation

Related How-To Topics

Last Updated: May 2010
[Return to How-To's Home]