Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

JColorChooser

Introduction to JColorChooser

JColorChooser offers a control panel that is designed to permit a user to choose a Color in RedGreenBlue(RGB) format. The control panel has two parts where one part is a tabbed pane to select colors and the other is the preview box. In the color chooser, five color choosers are present.

They are :

  • Swatches: To select the color from swatches.
  • HSV: To select the color using the Hue-Saturation-Value color representation
  • HSL: To select the color using the Hue-Saturation-Lightness color representation.
  • RGB: To select the color using the Red-Green-Blue color model.
  • CMYK: To select the color using the process color or four color model.

JColorChooser inherits the JComponent class. Syntax, different constructors, methods of JColorChooser is discussed in the following sections.

Syntax:

Syntax of JColorChooser is

public class JColorChooser extends JComponent implements Accessible

Following are the four fields of JLabel class:

  1. accessibleContext
  2. CHOOSER_PANELS_PROPERTY
  3. PREVIEW_PANEL_PROPERTY
  4. SELECTION_MODEL_PROPERTY

JColorChooser Constructor

Following are the constructors of JColorChooser. Since they are different depending on the parameters passed, they are used in different conditions.

  • JColorChooser(): A color chooser pane is created with a default white color.
  • JColorChooser(Color col): A color chooser pane is created with the mentioned initial color col.
  • JColorChooser(ColorSelectionModel m): A colour chooser pane is created with the mentioned ColorSelectionModel.

JColorChooser Methods

Following are the commonly used methods in JColorChooser.

  • addChooserPanel()
  • createDialog()
  • setColor
  • getAccessibleContext()
  • setColor(int r, int g, int b)
  • getChooserPanels()
  • showDialog()
  • getColor()
  • setSelectionModel(ColorSelectionModel mod)
  • getDragEnabled()
  • setDragEnabled()
  • setPreviewPanel(JComponent pr)
  • getPreviewPanel()
  • getSelectionModel()
  • getUI()
  • protected String paramString()
  • setUI()
  • updateUI()

Now, let us look into each of them in detail.

  • addChooserPanel(AbstractColorChooserPanel p)

A color chooser panel will be added to the color chooser.

  • JDialog createDialog(Component comp, String title, boolean modal, JColorChooser Cpane, ActionListener okListener, ActionListener cancelListener)

A new dialog box will be created and returns the mentioned ColorChooser pane with the buttons Cancel, Reset and OK. If OK or Cancel buttons are pressed by the user, then the dialog box will be automatically hidden. But it won’t be disposed of in that case. If the Reset button is pressed by the user, the color will be set to the last color when the show() was called last time. Parameters include the dialog box parent component, title name for the dialog box, a Boolean value where the program remainder is inactive till the dialog is closed, colorchooser pane, Ok Action listener, and Cancel action listener.

  • setColor(Color col)

The existing color of the color chooser will be set to the mentioned color.

  • AccessibleContext getAccessibleContext()

Returns the AccessibleContext related to the JColorChooser.

  • setColor(int r, int g, int b)

The existing color of the color chooser will be set to the mentioned RGB(Red, Green, Blue) color. Values of the red, green and blue color should be between the values 0 and 255(inclusive). In the parameters, r implies the amount of Red, g implies the amount of green and b implies the amount of blue.

  • AbstractColorChooserPanel[] getChooserPanels()

 Mentioned color panels will be returned.

  • showDialog(Component comp, String t, Color init_Col)

 A color chooser will be displayed and blocked until the dialog is hidden. i.e. if the OK button is pressed, then this method disposes or hides the dialog box and the chosen color will be returned. If the Cancel button is pressed or close is pressed without selecting the color, then null will be returned. Parameters include the dialog box parent component, title of the dialog box, and the initial color that is set when the color chooser is displayed.

  • Color getColor()

Returns the existing color value from the color-chooser.

  • setSelectionModel(ColorSelectionModel mod)

 A model containing the mentioned color will be set.

  • boolean getDragEnabled()

dragEnabled property’s value will be returned.

  • setDragEnabled()

This method helps in setting the dragEnabled property, that enables automatic drag handling by setting the value as true. The dragEnabled property default value is false. A drag and drop option will begin in Look and Feel when the user hovers the mouse button over the panel.

  • setPreviewPanel(JComponent pr)

The current preview panel will be set.

  • getPreviewPanel()

The preview panel with the chosen color will be returned.

  • getSelectionModel()

 The data model that manages the color will be returned.

  • getUI()

 L&F object that renders the component will be returned.

  • protected String paramString()

JColorChooser’s String representation will be returned.

  • setUI()

 L&F object that renders the component will be set.

  • updateUI()

L&F change notification will be triggered by the UIManager.

Program to implement JColorChooser

Now, let us see the program to implement Java JColorChooser. Several methods and constructor that are discussed in the above sections are also implemented in the given program

Code:

//Java program to implement JColorChooser
//import all the packages that are needed to implement JColorChooser
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JColorExample {
//declaration of labels, panels and frame
private JFrame mFrame;
private JLabel headerLbl;
private JLabel statusLbl;
private JPanel cntrlpnl;
//constructor of the class JColorExample
public JColorExample(){
GUIcreate(); //calls the function
}
//main method
public static void main(String[] args){
JColorExample  md = new JColorExample(); //object of the class is created
md.ColorChooserPane();//calls the function
}
//function for Graphical User Interface creation
private void GUIcreate(){
mFrame = new JFrame("Example for Java JColorChooser");
mFrame.setSize(600,600);//size of the frame is set
mFrame.setLayout(new GridLayout(3, 1)); //GridLayout is chosen as the layout for the frame
mFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
//layout description
headerLbl = new JLabel("", JLabel.CENTER);
statusLbl = new JLabel("",JLabel.CENTER);
statusLbl.setSize(350,100);
cntrlpnl = new JPanel();
cntrlpnl.setLayout(new FlowLayout());
mFrame.add(headerLbl);
mFrame.add(cntrlpnl);
mFrame.add(statusLbl);
mFrame.setVisible(true);
}
// function to display pane
private void ColorChooserPane(){
headerLbl.setText("Here it is: JColorChooser");
JButton chooseButton = new JButton("Choose the color to set as Background");
chooseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Color bg = JColorChooser.showDialog(mFrame,
"Choose background color", Color.white);
if(bg != null){
cntrlpnl.setBackground(bg);
mFrame.getContentPane().setBackground(bg);
}
}
});
cntrlpnl.add(chooseButton);
mFrame.setVisible(true);
}
}

Output:

When you click on that button, the following box appears.

After selecting the color, a screen appears as below.

Conclusion

JcolorChooser helps in offering a control panel that is considered to allow a user to select a color in the Red, Green, Blue(RGB) format as part of design purpose. As already discussed, the control panel created will have two parts in which one is a tabbed pane and the other is the preview box.

Recommended Articles

This is a guide to JColorChooser. Here we discuss the constructors and methods along with its program to implement JColorChooser. You may also look at the following articles to learn more-

  1. Constructor in Java
  2. 3D Arrays in Java
  3. BorderLayout in Java
  4. StringBuffer in Java

The post JColorChooser appeared first on EDUCBA.



This post first appeared on Best Online Training & Video Courses | EduCBA, please read the originial post: here

Share the post

JColorChooser

×

Subscribe to Best Online Training & Video Courses | Educba

Get updates delivered right to your inbox!

Thank you for your subscription

×