1. Computing

Button Overview

By

The Button Class

Overview

The Button class is used to create a simple button control on a graphical user interface. It can display a String, an image or both. A button control created from the Button class can be used as a regular button (i.e., you click on it and something happens) or can be used as the default button (i.e., if you hit enter whilst on the form then whatever happens for this button occurs) or the cancel button (i.e., like the default button but instead the escape key is pressed).

Import Statement

import javafx.scene.control.Button;

Constructors

The Button class has three constructors depending on whether you want to create a Button object with a String, an image or both:

  • To create an empty Button object:
    Button but = new Button();
  • To create a Button object with a String:
  • Button but = new Button("a String");
  • To create a Button object with a String and Image:
    //Use the javafx.scene.image.Image class to create an image 
    //object to reference the image for the Button.
    //This will actually be added to the Button as a Node - the ImageView
    //class takes care of this.
    Image image = new Image(getClass().getResourceAsStream("buttonimage.jpg"));
    Button but = new Button("a String", new ImageView(image));

Useful Methods

If you create an empty Button object the text an be set using the setText method:

but.setText("Another String");

Similarly, if you want to add an image to an existing button object use the setGraphic method:

Image image = new Image("buttonimage.jpg");
but.setGraphic(new ImageView(image));

There are corresponding methods getText and getGraphic which will return the String and Node used for the text and image of the Button:

String buttonString = but.getText();
Node buttonNode = but.getGraphic();

The color of the text of a Button can be set using the setTextFill method:

//A color is represented by the javafx.scene.paint.Paint class. 
//It takes a String representation using the javafx.scene.paint.Color class. 
//This can be through a constant value (e.g., Color.BLUE, Color. GREEN
but.setTextFill(Color.DARKGREEN);

Firing an Event When the Button is Clicked

A button is no use if nothing happens when it is clicked. For Button objects the EventHandler listens for an ActionEvent which defines exactly what happens when the button is clicked. To set up the EventHandler and ActionEvent use the setOnAction method:

but.setOnAction(new EventHandler<ActionEvent>() {
   @Override public void handle(ActionEvent e) {
   
   //Place the code you want to execute on the button click here.
   
   }
});

Usage Tips

As mentioned at the start a Button object can also be set to be the default button or cancel button for a JavaFX form. This means the ActionEvent defined by the setOnAction method will be executed on the press of the Enter key or ESC key.

To set up a Button as the default button use the setDefaultButton method:

but.setDefaultButton(true);

To set up a Button as the cancel button use the setCancelButton method:

but.setCancelButton(true);

A Button object can be checked to see if it is the default button or cancel button by using the isDefaultButton and isCancelButton methods.

To find out about other JavaFX controls have a look at JavaFX User Interface Controls.

©2014 About.com. All rights reserved.