This is a Java Program to Create a Color Dialog Box to Change the Background Color of Frame
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.
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 :
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.
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.
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.
/*Java Program to change color of frame using color dialog box*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Frame_Color implements ActionListener
{
static JFrame frame;
//Driver function
public static void main(String args[])
{
//Create a frame
frame = new JFrame("Change Frame Background");
frame.setSize(400,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.white);
frame.setLayout(new FlowLayout());
//Create an object
Frame_Color obj = new Frame_Color();
//Create a button
JButton button = new JButton("Change Color");
button.addActionListener(obj);
frame.add(button);
//Display the fame
frame.setVisible(true);
}
//Function to create color dialog box and change color
public void actionPerformed(ActionEvent e)
{
//Create a color dialog box
JColorChooser color_box= new JColorChooser();
Color color=color_box.showDialog(frame,"Select a Color",Color.white);
//Change background color of frame
frame.getContentPane().setBackground(color);
}
}
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
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.
Test case 2 – To change background color. (Selected color – Yellow)
Test case 3 – To change background color. (Selected color – Cyan)
Sanfoundry Global Education & Learning Series – Java Programs.
If you find any mistake above, kindly email to [email protected]- Apply for Java Internship
- Practice Programming MCQs
- Check Programming Books
- Practice Information Technology MCQs
- Check Java Books