Web
Analytics
Example of JColorChooser in Java Swings | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

JcolorChoose is a component of Java Swings which is used for choosing color from GUI application.

 

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;

class MainClass extends JFrame implements ChangeListener
{
	JColorChooser jc;
	Container c;
	MainClass()
	{
		//Set Size of Jframe
		this.setSize(500,500);
		//Make Jframe visible because by default it is invisible 
		this.setVisible(true);
		//Set Title of JFrame
		setTitle("Swings JColorChooser Example");
		//Set Default event to close Jframe window
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//Create a new Container for JColorChooser
		c=getContentPane();
		//Create Object for JColorJooser
		jc=new JColorChooser();
		//Add Listner for Color Choosing Action
		jc.getSelectionModel().addChangeListener(this);
		//Add Jcolorchooser component into Container
		c.add(jc);
		
	}
	public void stateChanged(ChangeEvent e)
	{
		//Get Color which is picked by end user
		Color color=jc.getColor();
		//Create New Jframe
		JFrame jf=new JFrame();
		jf.setSize(400,400);
		jf.setVisible(true);
		//Set Background Color of newly created Jframe
		jf.getContentPane().setBackground(color);
	}
	public static void main(String[] args)
	{
		MainClass obj=new MainClass();
	}
	
}

 Output

jcolorchooser_swings_example

jcolorfilechooser_swings_output