jcolorchooser
The JColorChooser
class in Java Swing provides a dialog box that enables users to select a color. It is a standard dialog that provides a convenient way for users to choose a color.
Here's an example of how to use JColorChooser
in Java:
import javax.swing.*; import java.awt.*; public class JColorChooserExample { public static void main(String[] args) { // Create a JFrame JFrame frame = new JFrame("JColorChooser Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Create a JButton and add an ActionListener JButton button = new JButton("Choose a Color"); button.addActionListener(e -> { // Display the color chooser dialog Color color = JColorChooser.showDialog(frame, "Choose a color", Color.WHITE); if (color != null) { // Set the background color of the button to the selected color button.setBackground(color); } }); // Add the button to the JFrame and pack the frame frame.getContentPane().add(button, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); } }
In this example, a JButton
is created and added to a JFrame
. When the button is clicked, a JColorChooser
dialog is displayed. The selected color is then set as the background color of the button.