javacodex.com
Java Examples
Java Examples
memu home questions

JColorChooser

A JColorChooser is a Swing component that lets you choose a color graphically in a JDialog. It provides a static method, as you have seen in a JOptionPane, which creates and shows a color chooser component in a JDialog.

Source: (JColorChooserExample.java)

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class JColorChooserExample {
 
  public static void main(String[] arguments) {
 
    final JButton button =new JButton("color");  
 
    button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        Color background = button.getBackground();
        Color c = JColorChooser.showDialog(null, "ZZZZ", background);
        if (background != null) {
          button.setBackground(c);
        }
      }
    });
 
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame f = new JFrame("JColorChooser Example");
    f.setLayout(new FlowLayout());  
    f.setSize(300,100);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
    f.add(button);
    f.setVisible(true);
  }
}
 

Output:

$ java JColorChooserExample
Output



Contact: javacodex@yahoo.com