JColorChooser Component in Java

Today, i will teach you how to create a program that has the JColorChooser component in Java. We have ColorDialog in VB.NET and C# and this is equivalent to the JColorChooser in Java. So, now let's start this tutorial! 1. Open JCreator or NetBeans and make a java program with a file name of colorChooser.java. 2. Import java.awt.* and javax.swing.* packages because we will going to have the JColorChooser component in swing and also the JPanel as the container of this.
  1. import javax.swing.*;
  2. import java.awt.*;
3. Creates an instance of JColorChooser and set Color.RED as the default selected color and named it as jcc. This will be below in your public class colorChooser extends JFrame .
4. Create a constructor that is the same with the filename and create a panel out of there to hold the JColorChooser and its border.
  1. JPanel content = new JPanel();
  2.  content.setLayout(new BorderLayout());
  3.  content.add(jcc, BorderLayout.CENTER);
Set window characteristics of the program. This will also add the panel and the jcolorchooser with its title and closing event.
  1.         this.setContentPane(content);    
  2.         this.setTitle("JColorChooser Component");
  3.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  4.         this.pack();
5. In your Main, create a JFrame component that will hold the panel and the JColorChooser component and all other components. This will set the visibility to True.
  1.     public static void main(String[] args) {
  2.          JFrame chooser = new colorChooser();
  3.        chooser.setVisible(true);
  4.     }
Press F5 and run the program. Output: output output output Full source code:
  1. import javax.swing.*;
  2. import java.awt.*;
  3.  
  4. public class colorChooser extends JFrame {
  5.          JColorChooser jcc = new JColorChooser(Color.RED);
  6.          
  7.          
  8.     public colorChooser() {
  9.  
  10.                 JPanel content = new JPanel();
  11.         content.setLayout(new BorderLayout());
  12.        
  13.          content.add(jcc, BorderLayout.CENTER);
  14.        
  15.        
  16.         this.setContentPane(content);    
  17.         this.setTitle("JColorChooser Component");
  18.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.         this.pack();
  20.     }
  21.  
  22.    
  23.     public static void main(String[] args) {
  24.          JFrame chooser = new colorChooser();
  25.  
  26.        chooser.setVisible(true);
  27.     }
  28. }
Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment