Java Program to Create a Color Dialog Box to Change the Background Color of Frame

This is a Java Program to Create a Color Dialog Box to Change the Background Color of Frame

Problem Description

We have to write a program in Java such that it creates a button to open a color dialog box from where the user is able to select any color and the selected color is set as the background color of the frame.

Expected Input and Output

For changing color of frame using a color dialog box, we have the following different sets of input and output.

1. When the frame is created :

On the execution of the program,
it is expected that a frame appears with a button to open the color dialog box.

2. When a color is chosen :

advertisement
advertisement
When the button is clicked,
it is expected that a color dialog box appears and the user can select a color,
and the selected color becomes the background color of the frame.
suppose if we select the color Yellow.
then it is expected that the background color of the frame change to Yellow.
Problem Solution

1. Create a frame and a button.
2. Add ActionListener with the button.
3. When the button is clicked, create a color dialog box.
4. Get the selected color and change the background color of the frame.

Program/Source Code

Here is source code of the Java Program to change color of frame using color dialog box. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

Note: Join free Sanfoundry classes at Telegram or Youtube
  1. /*Java Program to change color of frame using color dialog box*/
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. class Frame_Color implements ActionListener
  6. {
  7.     static JFrame frame;
  8.     //Driver function
  9.     public static void main(String args[])
  10.     {
  11. 	//Create a frame
  12. 	frame = new JFrame("Change Frame Background");
  13. 	frame.setSize(400,400);
  14. 	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  15. 	frame.getContentPane().setBackground(Color.white);
  16. 	frame.setLayout(new FlowLayout());
  17. 	//Create an object
  18. 	Frame_Color obj = new Frame_Color();
  19. 	//Create a button
  20. 	JButton button = new JButton("Change Color");
  21. 	button.addActionListener(obj);
  22. 	frame.add(button);	
  23. 	//Display the fame
  24. 	frame.setVisible(true);
  25.     }
  26.     //Function to create color dialog box and change color
  27.     public void actionPerformed(ActionEvent e)
  28.     {
  29. 	//Create a color dialog box
  30. 	JColorChooser color_box= new JColorChooser();
  31. 	Color color=color_box.showDialog(frame,"Select a Color",Color.white);
  32. 	//Change background color of frame
  33. 	frame.getContentPane().setBackground(color);
  34.     }
  35. }
Program Explanation

1. To create a color dialog box use JColorChooser class.
2. Display the color dialog box using showDialog(Component,title,initial color) function.
3. Update the color of the frame

Runtime Test Cases

Here’s the run time test cases for changing color of frame using color dialog box.

advertisement

Test case 1 – To display the frame with button and dialog box.
java-program-color-chooser-frame

Test case 2 – To change background color. (Selected color – Yellow)
java-program-color-chooser-yellow

Test case 3 – To change background color. (Selected color – Cyan)
java-program-color-chooser-green

advertisement

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.