In this example we are going to see how to use Java JColorChooser Example.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.awt.event.*; import java.awt.*; import javax.swing.*; public class Main extends JFrame implements ActionListener { JButton b; Container c; Main() { c = getContentPane(); c.setLayout(new FlowLayout()); b = new JButton("color"); b.addActionListener(this); c.add(b); } public void actionPerformed(ActionEvent e) { Color initialcolor = Color.RED; Color color = JColorChooser.showDialog(this, "Select a color", initialcolor); c.setBackground(color); } public static void main(String[] args) { Main ch = new Main(); ch.setSize(400, 400); ch.setVisible(true); ch.setDefaultCloseOperation(EXIT_ON_CLOSE); } } |
Output:
The JColorChooser class is used to create a color chooser dialog box so that user can select any color.
JColorChooser(): is used to create a color chooser pane with white color initially.
JColorChooser(Color initialColor): is used to create a color chooser pane with the specified color initially.
public static Color showDialog(Component c, String title, Color initialColor): is used to show the color-chooser dialog box.