The Use of The Color Chooser (JColorChooser) and do - while Statement in Java Using Eclipse IDE
What Will I Learn?
In this tutorial;
- You will learn how to use the JColorChooser class.
- You will learn how to use the chosen color in color choser.
- You will learn the use of do - while statement.
Requirements
- Java SE Development Kit (Not necessarily the last version) is required.
- Eclipse IDE or any similar Interated Development Enviroment (IDE) that is designed for Java programming language is required.
Difficulty
- Intermediate
Tutorial Contents
The purpose of this tutorial is to show the use of color choosers in Java. The demo program for the color chooser contains a single Java file (Color_Choosers_Demo.java). The following figure contains the code of the Java file.
In this demo program, it is expected to see a color chooser in the screen, when the program run. Then, the chosen color in this color choser determines the background color of the panel of the frame that opens after choosing a color.
To achieve these in the demo program, firstly we imported the neccessary GUI packages, defined our Java file and constructed our frame as usual with the following code.
import javax.swing.*;
import java.awt.*;
public class Color_Choosers_Demo {
public static void main(String[] args) {
JFrame frame = new JFrame("Color Choosers Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Then, we need a panel to add this frame. The chosen color will determine its background color later on. With the following code, a panel with white background color and the size of 200 rows and 250 columns is created.
JPanel colorp = new JPanel();
colorp.setBackground(Color.WHITE);
colorp.setPreferredSize(new Dimension(250, 200));
With the folowing piece of code, the job is finished with constructing frame.
frame.getContentPane().add(colorp);
frame.pack();
frame.setVisible(true);
Now, we need to create a color object, which is white initially, to use the chosen color. In addition to this, an integer object is neccesary to check whether the user wants to repeat the program.
Color chosen_color = Color.WHITE;
int repeat;
The do - while Statement
The do statement is used with while statement. The compiler starts to execute the code in the do statement. Then, the compiler checks the while statement. If the condition in while statement is true, the compile again goes to the code in the do statement. Unless the condition in the while returns false, the code in do statement is executed again and again. For instance, the following code opens a confirm dialog and ask the user whether continue to ask or not. If the user selects yes, the statement in while returns true and again confirm dialog code is executed. This process continiues until user selects no.
do {
repeat = JOptionPane.showConfirmDialog(null, "New Color?");
}
while (repeat == JOptionPane.YES_OPTION);
JColorChooser
The JColorChooser class represents a color chooser. It allows us to display a special dialog box that is prepared as color chooser. It can be directly used with the showDialog method. It takes the title of the dialog box, the frame and the initial color as parameters. If we want to see the color chooser dialog box again and again, we need to add its code in the do statement like in the following code;
do {
chosen_color=JColorChooser.showDialog(frame, "Choose a Color", chosen_color);
colorp.setBackground(chosen_color);
repeat = JOptionPane.showConfirmDialog(null, "New Color?");
}
while (repeat == JOptionPane.YES_OPTION);
The color chooser changes the initial color with the choosen color. Thus we can change the background color of the panel.
Results
When you run the program, the frame (left upper corner) and the color chooser opens.
You can choose any color you want. If you need to choose more complicated color you can choose RGB or other color representation techniques from the tabbed pane. We choose a green color like the following figure;
When the OK button pressed the background color of the frame changes and the program ask user to whether change color again or not.
When you press the Yes button, the color chooser opens again and you change the color again.
Github
You can get this program from here.
Code of The Program
import javax.swing.*;
import java.awt.*;
public class Color_Choosers_Demo {
public static void main(String[] args) {
JFrame frame = new JFrame("Color Choosers Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel colorp = new JPanel();
colorp.setBackground(Color.WHITE);
colorp.setPreferredSize(new Dimension(250, 200));
frame.getContentPane().add(colorp);
frame.pack();
frame.setVisible(true);
Color chosen_color = Color.WHITE;
int repeat;
do {
chosen_color=JColorChooser.showDialog(frame, "Choose a Color", chosen_color);
colorp.setBackground(chosen_color);
repeat = JOptionPane.showConfirmDialog(null, "New Color?");
}
while (repeat == JOptionPane.YES_OPTION);
}
}
Curriculum
You can find my other java related tutorials in below links.
- Adding JPEG or GIF image files into the GUI in Java using Eclipse IDE
- Reading Text Files and Parsing It Using Iterators and Delimiter in Java with Eclipse IDE
- Drawing Methods in Java Applet (JApplet) Using Eclipse IDE: Drawing a Snowman
- Check Boxes (JCheckBox) and Its Listeners in a Java GUI using Eclipse IDE
- Use of Layout Managers and Tabbed Pane in Java Using Eclipse IDE
- Use of Borders in a Java GUI Using Eclipse IDE
- The Use of File Choosers in Java Using Eclipse IDE
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @kizilelma, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!
Hey @aromatheraphy I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x