JavaFX Color Picker

0

JavaFX Color Picker Tutorials:

JavaFX Color Picker enables user to select a particular color from available range or set a custom color.

The ColorPicker consists of the color chooser, color palette, and custom color window.

Color Chooser :

A combo box which indicate the current color.

Constructors of color picker :

  • ColorPicker()Creates a default ColorPicker instance with a selected color set to white.
  • ColorPicker(Color color)Creates a ColorPicker instance and sets the selected color to the given color.

Creating ColorPicker :

  • To create a color picker with white color selected.

    ColorPicker colorPicker = new ColorPicker();

  • To create a color picker with RED Color selected ASColorPicker colorPicker = new ColorPicker(Color.RED);

Color picker example:


package javafxtuts;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ColorPicker;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Javafxtuts extends Application {

  public static void main(String[] args) {
     Application.launch(args);
  }

  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      ColorPicker colorPicker = new ColorPicker();
      colorPicker.setValue(Color.GREEN);
      
      BorderPane root = new BorderPane();
      root.setTop(colorPicker);
      root.setPadding(new Insets(10, 10, 10, 10));
        Scene scene = new Scene(root, 300, 200);
        stage.setScene(scene);
        stage.show();
  }
}

Output:

Color picker example Explanation :

  • Creating a ColorPicker AsColorPicker colorPicker = new ColorPicker();
  • colorPicker.setValue(Color.GREEN);It set the current value of the color to GREEN.
  • BorderPane root = new BorderPane();
    root.setTop(colorPicker);
    It set top of BorderPane Layout with ColorPicker.
  • root.setPadding(new Insets(10, 10, 10, 10));It set padding to the root(BorderPane).
Share.

Leave A Reply