JavaFX ColorPicker

Jakob Jenkov
Last update: 2019-07-26

The JavaFX ColorPicker control enables the user to choose a color in a popup dialog. The chosen color can later be read from the ColorPicker by your JavaFX application. The JavaFX ColorPicker control is represented by the class javafx.scene.control.ColorPicker. Here is a screenshot of an opened JavaFX ColorPicker:

JavaFX ColorPicker screenshot.

Full ColorPicker Example

Here is a full JavaFX ColorPicker example so you can see what the code looks like:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ColorPickerExample extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX App");

        ColorPicker colorPicker = new ColorPicker();

        Color value = colorPicker.getValue();

        VBox vBox = new VBox(colorPicker);
        //HBox hBox = new HBox(button1, button2);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

}

Create a ColorPicker

In order to use a JavaFX ColorPicker you must first create an instance of the ColorPicker class. Here is an example of creating a JavaFX ColorPicker:

ColorPicker colorPicker = new ColorPicker();

Get Chosen Color

To read the color chosen in a JavaFX ColorPicker you call its getValue() method. Here is an example of getting the chosen color in a JavaFX ColorPicker:

Color value = colorPicker.getValue();

Jakob Jenkov

Featured Videos

Java ForkJoinPool

P2P Networks Introduction




















Advertisements

High-Performance
Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Previous
Next tutorial in this tutorial trail
Next