Java Swing GUI

JColorChooser – Java Swing – Example

In this tutorial, we are going to see an example of JColorChooser in Java Swing. JColorChooser class is used to create a dialog box that allows color selection so that the user can select any color. It inherits from JComponent class.


JColorChooser constructors class:
JColorChooser Constructors
JColorChooser() It is used to create a color selection panel with a white color initially.
JColorChooser(color init) It is used to create a color selection panel with the initially specified color.


Commonly used methods:
  • addChooserPanel(AbstractColorChooserPanel panel) : It is used to add a color selection panel to the color picker.
  • Color showDialog(Component c, String title, Color initialColor) : It is used to display the color picker dialog box.



Example of JColorChooser in Java Swing:
import java.awt.event.*;  
import java.awt.*;  
import javax.swing.*;

public class ChooseColor extends JFrame implements ActionListener
  Container container;

    container = getContentPane();
    container.setLayout(new FlowLayout());
    JButton btn = new JButton("Color");

  public void actionPerformed(ActionEvent e)
    Color init = Color.BLUE;
    Color coleur = JColorChooser.showDialog(this,"Choose a color",init);
  public static void main(String[] args)
    ChooseColor frame = new ChooseColor();


mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *