Pages - GamesandApps

Java Code to create Color chooser inside your program

Java Code to create Color chooser inside your program - Output
Output - Color Chooser Program in Java

Alright! If you are searching for a way to help your users to customize the java program by using various color options inside the program, then here is a way. Add this code inside your program to bring out the various color modes. If you are a good java programmer, then you should be able to break the code and use it in your java program easily. This java code has three different ways to choose the color for your program. You can choose from the Swatches, HSB or the RGB tabs. The code is written below. Copy and paste the code below in italics and add to a text document and name it as ColorSample_menu.java. Then compile the class as ColorSample_menu. 



// Display the Color Chooser program by GamesAndApps

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

public class ColorSample_menu extends JFrame implements ActionListener
{

 JMenuBar jm;
 JMenu color;
 JMenuItem col,exit;
 JColorChooser jcol; 
 Container con;

 ColorSample_menu()
 {
  con=getContentPane();
   jm = new JMenuBar();
    color =  new JMenu("Color");
  col = new JMenuItem("Select Color");
  exit = new JMenuItem("Exit");
  jcol = new JColorChooser();
  col.addActionListener(this);
  exit.addActionListener(this); 

  setJMenuBar(jm);
  jm.add(color);
  color.add(col);
  color.add(exit);
  
  setSize(200,300);
  setVisible(true);
 }  
  
 public void actionPerformed(ActionEvent ae)
 {
  Object o = ae.getSource();
  
  if(o==exit)
  {
   System.exit(0);
  }

  if(o==col)
  {
    Color in = getBackground();
    Color background = JColorChooser.showDialog(null,"JColorChooser Sample",in);
      if (background != null)             
   {
    con.setBackground(background);
    }

  } 

 }
  
  public static void main(String args[]) 
 {
  new ColorSample_menu();    
 }




The program given above should work fine. If you face any problems, then leave a comment and let me know.

1 comment:

  1. Hello, sir i would like to ask that what is the scope of java training, what all topics should be covered and it is kinda bothering me … and has anyone studies from this course http://www.wiziq.com/course/1779-core-and-advance-java-concepts of core and advance java online ?? or tell me any other guidance...
    would really appreciate help… and Also i would like to thank for all the information you are providing on java concepts.

    ReplyDelete