JavaFX ColorPicker Example

In this quick tutorial, we will see how to use the JavaFX ColorPicker control with an example.

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.

JavaFX ColorPicker Example

In this example, we have a ColorPicker and a Text control. The selected color from the color picker is used to set the foreground color of the text control.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.HBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

    public void start(Stage stage) {


    private void initUI(Stage stage) {

        var root = new HBox(25);
        root.setPadding(new Insets(10));

        var txt = new Text("ZetCode");

        var font = Font.font(20);

        var cp = new ColorPicker();
        cp.setOnAction((ActionEvent event) -> txt.setFill(cp.getValue()));

        root.getChildren().addAll(cp, txt);

        var scene = new Scene(root, 300, 250);


    public static void main(String[] args) {

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours

Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course