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.

advertisement

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 :

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.

advertisement
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.

  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

advertisement
Runtime Test Cases

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

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.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn