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


You can create controls in the following ways:

Use Built-In UI Controls

Built-in UI controlsBuilt-in UI controls are nodes with some extra properties.The following built-in control classes from the javafx.scene.control package can be used to create UI controls:

  • TextBox
    Displays and accepts input of text. (TextBox API)
  • Button
    Invokes a function when clicked. (Button API)
  • ToggleButton
    A control with a Boolean variable indicating whether it has been selected. (ToggleButton API)
  • ToggleGroup
    Manages a group of toggle buttons so that only one can be selected at a time. Unlike in the RadioButton class, having no toggle buttons selected is acceptable. (ToggleGroup API)
  • RadioButton
    Requires one and only one state to be selected. (RadioButton API)
  • CheckBox
    Has three states: checked, unchecked, undefined. (CheckBox API)
  • Hyperlink
    Enables links to URLs. (Hyperlink API)
  • Slider
    Enables the user to slide a control forward or backward to jump ahead or jump back in some way. (Slider API)
  • ProgressBar
    Can indicate degree of progress or swish back and forth to indicate that progress is occurring. (ProgressBar API)
  • ProgressIndicator
    Same functionality as the progress bar but with a circle instead of a bar. (ProgressIndicator API)
  • ListView
    Items are displayed in a list box, from which the user can select. If the list exceeds a certain length, a scrollbar is displayed. (ListView API)
  • Label
    A noneditable text control. Used as the label for the controls displayed in the screen capture. (Label API)
  • ScrollBar (not shown in screen capture)
    A standard scrollbar that enables users to move the view. (ScrollBar API)
  • Keystroke (not shown in screen capture)
    Invokes an event based on keystroke entry. (Keystroke API)

Examples of Built-In Controls

Integrate Swing Components

You can add controls by integrating Swing components into your JavaFX application. See the following links for more information.

Create Custom Controls Programmatically

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

  • Extend CustomNode ( as the example shows)
  • Extend the Control class and provide a custom skin.

Both approaches can be customized by applying stylesheets. The following code fragment illustrates the first approach by creating a custom UI control with a label:

class MyControl extends CustomNode {

  var text: String;
  var control: Node[];
  override function create() {
    HBox {
      spacing: 5
      nodeVPos: VPos.CENTER
      content: bind [
        control,
        Label {
          text: text
          textFill: Color.WHITESMOKE
          font: Font {name: "Arial"}
        }
      ]
    }
  }
}

Consider how this customized control can be applied to create two radio buttons:

var toggleGroup = MyToggleGroup{};
var button1 = MyControl {
          content: RadioButton { toggleGroup: toggleGroup selected: true id: "Yes"}
          text: "Yes"
};

var button2 = MyControl {
          content: RadioButton { toggleGroup: toggleGroup selected: true id: "No"}
          text: "No"
};

When integrated into an application, this code fragment produces the following radio buttons.

custom radio buttons

For an example of creating controls by extending CustomNode, 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 creating controls by extending the Control class, see the following link:

API Documentation

Related How-To Topics

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



Rate This Article
Leave a Comment or Suggest a Topic

Do you have comments about this article? We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies—your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use.

 

English
日本語
한국어
简体中文
русский
Português do Brasil