Open In App

JavaFx | ColorPicker with examples

Last Updated : 14 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

ColorPicker is a part of JavaFX. ColorPicker allows the user to choose a color from given set of colors or make their own custom color. An initial Color can be set using the setValue() function or defining it in a constructor and the color selected by the user can be found using the getValue() function.
An Action event is generated when the user selects a color from the color picker. This event can be handled using an event handler.

ColorPicker appearance can be controlled in three ways:

  • A simple button mode
  • A menu button mode
  • A split menu button mode
  • Constructor of the class are:

    1. ColorPicker():Creates a default ColorPicker instance with a selected color set to white.
    2. ColorPicker(Color c):Creates a ColorPicker instance and sets the selected color to the given color.

    Commonly used methods:

    method explanation
    getCustomColors() Gets the list of custom colors added to the Color Palette by the user.
    setValue(Color c) sets the color of the color picker to color c
    getValue() returns a color object that defines the color selected by the user

    Below programs will illustrate the use of color picker:

    1. Program to create a color picker and add it to the stage: This program creates a ColorPicker indicated by the name cp. The color picker will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage.Then a tile-pane is created, on which addChildren() method is called to attach the color picker inside the scene, along with the resolution specified by (200, 200) in the code. Finally, the show() method is called to display the final results.




      // Java Program to create a color picker and add it to the stage
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.*;
      import javafx.scene.layout.*;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.collections.*;
      import javafx.stage.Stage;
      import javafx.scene.text.Text.*;
      import javafx.scene.paint.*;
      import javafx.scene.text.*;
      public class colorpicker extends Application {
          // labels
          Label l;
        
          // launch the application
          public void start(Stage s)
          {
              // set title for the stage
              s.setTitle("creating color picker");
        
              // create a tile pane
              TilePane r = new TilePane();
        
              // create a label
              l = new Label("This is a color picker example ");
        
              // create a color picker
              ColorPicker cp = new ColorPicker(Color.BLUE);
        
              // add label
              r.getChildren().add(l);
              r.getChildren().add(cp);
        
              // create a scene
              Scene sc = new Scene(r, 200, 200);
        
              // set the scene
              s.setScene(sc);
        
              s.show();
          }
        
          public static void main(String args[])
          {
              // launch the application
              launch(args);
          }
      }

      
      

      Output:

    2. Program to create color picker of three different appearance: This program creates a ColorPicker indicated by the name cp, cp1, cp2. cp will have the appearance of menu button, cp1 will have the appearance of button and cp2 will have the appearance of the split button. The color pickers will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. Then a tile-pane is created, on which addChildren() method is called to attach the color picker inside the scene, along with the resolution specified by (200, 200) in the code. Finally, the show() method is called to display the final results.




      // Java Program to create color picker of three different appearance
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.*;
      import javafx.scene.layout.*;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.collections.*;
      import javafx.stage.Stage;
      import javafx.scene.text.Text.*;
      import javafx.scene.paint.*;
      import javafx.scene.text.*;
      public class colorpicker_1 extends Application {
          // labels
          Label l;
        
          // launch the application
          public void start(Stage s)
          {
              // set title for the stage
              s.setTitle("creating color picker");
        
              // create a tile pane
              TilePane r = new TilePane();
        
              // create a label
              l = new Label("This is a color picker example ");
        
              // create a color picker
              ColorPicker cp = new ColorPicker(Color.BLUE);
        
              // create a color picker
              ColorPicker cp1 = new ColorPicker(Color.BLUE);
        
              // set the appearance of color picker to  button
              cp1.getStyleClass().add("button");
        
              // create a color picker
              ColorPicker cp2 = new ColorPicker(Color.BLUE);
        
              // set the appearance of color picker to split button
              cp2.getStyleClass().add("split-button");
        
              // add label
              r.getChildren().add(l);
              r.getChildren().add(cp);
              r.getChildren().add(cp1);
              r.getChildren().add(cp2);
        
              // create a scene
              Scene sc = new Scene(r, 200, 200);
        
              // set the scene
              s.setScene(sc);
        
              s.show();
          }
        
          public static void main(String args[])
          {
              // launch the application
              launch(args);
          }
      }

      
      

      Output:

    3. Program to create color picker and add a listener to it: This program creates a ColorPicker indicated by the name cp .we will create an event hab=ndler and a label l2 that will show the color selected by the user. The event handler will handle the events of the colorpicker and will set the text of the label l2 to the RGB value of the color selected. The event will be associated with the color picker using setOnAction() method. The color pickers will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. Then a tile-pane is created, on which addChildren() method is called to attach the color picker inside the scene, along with the resolution specified by (200, 200) in the code. Finally, the show() method is called to display the final results.




      // Java Program to create  color picker and add listener to it
      import javafx.application.Application;
      import javafx.scene.Scene;
      import javafx.scene.control.*;
      import javafx.scene.layout.*;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.collections.*;
      import javafx.stage.Stage;
      import javafx.scene.text.Text.*;
      import javafx.scene.paint.*;
      import javafx.scene.text.*;
      public class colorpicker_2 extends Application {
          // labels
          Label l;
        
          // launch the application
          public void start(Stage s)
          {
              // set title for the stage
              s.setTitle("creating color picker");
        
              // create a tile pane
              TilePane r = new TilePane();
        
              // create a label
              l = new Label("This is a color picker example ");
              Label l1 = new Label("no selected color ");
        
              // create a color picker
              ColorPicker cp = new ColorPicker();
        
              // create a event handler
              EventHandler<ActionEvent> event = new EventHandler<ActionEvent>() {
                  public void handle(ActionEvent e)
                  {
                      // color
                      Color c = cp.getValue();
        
                      // set text of the label to RGB value of color
                      l1.setText("Red = " + c.getRed() + ", Green = " + c.getGreen() 
                                                       + ", Blue = " + c.getBlue());
                  }
              };
        
              // set listener
              cp.setOnAction(event);
        
              // add label
              r.getChildren().add(l);
              r.getChildren().add(cp);
              r.getChildren().add(l1);
        
              // create a scene
              Scene sc = new Scene(r, 500, 200);
        
              // set the scene
              s.setScene(sc);
        
              s.show();
          }
        
          public static void main(String args[])
          {
              // launch the application
              launch(args);
          }
      }

      
      

      Output:

      Note: The above programs might not run in an online IDE please use an offline compiler.
      Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/ColorPicker.html



      Like Article
      Suggest improvement
      Share your thoughts in the comments

    Similar Reads