create account

The Use of The Color Chooser (JColorChooser) and do - while Statement in Java Using Eclipse IDE by aromatheraphy

View this thread on: hive.blogpeakd.comecency.com
· @aromatheraphy · (edited)
$32.79
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.
![code.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516787479/jlinvmczzttfls4sx3ri.jpg)
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.
![r1.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516798905/lvmtgcteozhroypidq7b.jpg)
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;

![r2.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799027/zsdkcegtavtmxevdhxxj.jpg)

When the OK button pressed the background color of the frame changes and the program ask user to whether change color again or not.

![r3.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799101/wybzqrxxquilk0xdgpir.jpg)
![r4.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799108/qiuz7bccgxilesimxntf.jpg)

When you press the Yes button, the color chooser opens again and you change the color again.

![Ekran Alıntısı.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799150/mw8mo8phauwzszls8idq.jpg)

##### Github
You can get this program from [here](https://github.com/aromatheraphy/Color_Choosers_Demo/blob/master/src/Color_Choosers_Demo.java).
![git.JPG](https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799390/k8vrypumjilfctxyjq5m.jpg)
##### 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](https://steemit.com/utopian-io/@aromatheraphy/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](https://steemit.com/utopian-io/@aromatheraphy/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](https://steemit.com/utopian-io/@aromatheraphy/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](https://steemit.com/utopian-io/@aromatheraphy/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](https://steemit.com/utopian-io/@aromatheraphy/use-of-layout-managers-and-tabbed-pane-in-java-using-eclipse-ide)
- [Use of Borders in a Java GUI Using Eclipse IDE](https://steemit.com/utopian-io/@aromatheraphy/use-of-borders-in-a-java-gui-using-eclipse-ide)
- [The Use of File Choosers in Java Using Eclipse IDE](https://steemit.com/utopian-io/@aromatheraphy/the-use-of-file-choosers-in-java-using-eclipse-ide)

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@aromatheraphy/the-use-of-the-color-chooser-jcolorchooser-and-do-while-statement-using-eclipse-ide">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , ,
properties (23)
authoraromatheraphy
permlinkthe-use-of-the-color-chooser-jcolorchooser-and-do-while-statement-using-eclipse-ide
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":32935745,"name":"che","full_name":"eclipse/che","html_url":"https://github.com/eclipse/che","fork":false,"owner":{"login":"eclipse"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","java","tr","eclipse"],"users":["aromatheraphy"],"links":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1516787479/jlinvmczzttfls4sx3ri.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516798905/lvmtgcteozhroypidq7b.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799027/zsdkcegtavtmxevdhxxj.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799101/wybzqrxxquilk0xdgpir.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799108/qiuz7bccgxilesimxntf.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799150/mw8mo8phauwzszls8idq.jpg","https://github.com/aromatheraphy/Color_Choosers_Demo/blob/master/src/Color_Choosers_Demo.java","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799390/k8vrypumjilfctxyjq5m.jpg","https://steemit.com/utopian-io/@aromatheraphy/adding-jpeg-or-gif-image-files-into-the-gui-in-java-using-eclipse-ide","https://steemit.com/utopian-io/@aromatheraphy/reading-text-files-and-parsing-it-using-iterators-and-delimiter-in-java-with-eclipse-ide","https://steemit.com/utopian-io/@aromatheraphy/drawing-methods-in-java-applet-japplet-using-eclipse-ide-drawing-a-snowman","https://steemit.com/utopian-io/@aromatheraphy/check-boxes-jcheckbox-and-its-listeners-in-a-java-gui-using-eclipse-ide","https://steemit.com/utopian-io/@aromatheraphy/use-of-layout-managers-and-tabbed-pane-in-java-using-eclipse-ide","https://steemit.com/utopian-io/@aromatheraphy/use-of-borders-in-a-java-gui-using-eclipse-ide","https://steemit.com/utopian-io/@aromatheraphy/the-use-of-file-choosers-in-java-using-eclipse-ide"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1516787479/jlinvmczzttfls4sx3ri.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516798905/lvmtgcteozhroypidq7b.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799027/zsdkcegtavtmxevdhxxj.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799101/wybzqrxxquilk0xdgpir.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799108/qiuz7bccgxilesimxntf.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799150/mw8mo8phauwzszls8idq.jpg","https://res.cloudinary.com/hpiynhbhq/image/upload/v1516799390/k8vrypumjilfctxyjq5m.jpg"],"moderator":{"account":"kizilelma","time":"2018-01-25T15:06:48.652Z","reviewed":true,"pending":false,"flagged":false}}
created2018-01-24 13:12:21
last_update2018-01-25 18:34:36
depth0
children3
last_payout2018-01-31 13:12:21
cashout_time1969-12-31 23:59:59
total_payout_value22.717 HBD
curator_payout_value10.076 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,439
author_reputation657,783,633,007
root_title"The Use of The Color Chooser (JColorChooser) and do - while Statement in Java Using Eclipse IDE"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id31,922,037
net_rshares3,842,019,052,709
author_curate_reward""
vote details (4)
@kizilelma ·
$1.65
Thank you for the contribution. It has been approved.

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
👍  
properties (23)
authorkizilelma
permlinkre-aromatheraphy-the-use-of-the-color-chooser-jcolorchooser-and-do-while-statement-using-eclipse-ide-20180125t150650631z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-25 15:07:03
last_update2018-01-25 15:07:03
depth1
children1
last_payout2018-02-01 15:07:03
cashout_time1969-12-31 23:59:59
total_payout_value1.291 HBD
curator_payout_value0.364 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length172
author_reputation7,465,931,045,199
root_title"The Use of The Color Chooser (JColorChooser) and do - while Statement in Java Using Eclipse IDE"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,224,120
net_rshares178,443,877,354
author_curate_reward""
vote details (1)
@utopian.tip ·
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!
properties (22)
authorutopian.tip
permlinkre-re-aromatheraphy-the-use-of-the-color-chooser-jcolorchooser-and-do-while-statement-using-eclipse-ide-20180125t150650631z-20180125t153358
categoryutopian-io
json_metadata""
created2018-01-25 15:34:00
last_update2018-01-25 15:34:00
depth2
children0
last_payout2018-02-01 15:34:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length159
author_reputation238,310,597,885
root_title"The Use of The Color Chooser (JColorChooser) and do - while Statement in Java Using Eclipse IDE"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,229,985
net_rshares0
@utopian-io ·
### Hey @aromatheraphy I am @utopian-io. I have just upvoted you!
#### Achievements
- You have less than 500 followers. Just gave you a gift to help you succeed!
- Seems like you contribute quite often. AMAZING!
#### Suggestions
- Contribute more often to get higher and higher rewards. I wish to see you often!
- Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
#### Get Noticed!
- Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!
#### Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. <a href="https://discord.gg/zTrEMqB">Participate on Discord</a>. Lets GROW TOGETHER!
- <a href="https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1">Vote for my Witness With SteemConnect</a>
- <a href="https://v2.steemconnect.com/sign/account-witness-proxy?proxy=utopian-io&approve=1">Proxy vote to Utopian Witness with SteemConnect</a>
- Or vote/proxy on <a href="https://steemit.com/~witnesses">Steemit Witnesses</a>

[![mooncryption-utopian-witness-gif](https://steemitimages.com/DQmYPUuQRptAqNBCQRwQjKWAqWU3zJkL3RXVUtEKVury8up/mooncryption-s-utopian-io-witness-gif.gif)](https://steemit.com/~witnesses)

**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**
properties (22)
authorutopian-io
permlinkre-aromatheraphy-the-use-of-the-color-chooser-jcolorchooser-and-do-while-statement-using-eclipse-ide-20180126t070624596z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-01-26 07:06:27
last_update2018-01-26 07:06:27
depth1
children0
last_payout2018-02-02 07:06:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,511
author_reputation152,955,367,999,756
root_title"The Use of The Color Chooser (JColorChooser) and do - while Statement in Java Using Eclipse IDE"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id32,399,888
net_rshares0