Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-09-2008, 06:52 AM
Member
 
Join Date: Jul 2008
Posts: 4
behrk2 is on a distinguished road
JButton onClick change color background
Hey guys,

I have an array of six colors. I also have a button. What I want to do is, everytime the button is clicked, have its background color change to the next index of the array. Once the button has changed to all six colors, I want it to repeat.

Here is the code I have:

Code:
private void gboard_row1_btn1ActionPerformed(java.awt.event.ActionEvent evt) { Color colors[] = new Color[6]; int i=0; colors[0] = Color.red; colors[1] = Color.orange; colors[2] = Color.yellow; colors[3] = Color.green; colors[4] = Color.blue; colors[5] = new Color(138, 43, 226); while( i<6 ) { gboard_row1_btn1.setBackground(colors[i]); i++; } }
My loop is screwed up and I don't know how I would go about implementing a solution for this. Does anyone have any ideas?

Thanks!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-09-2008, 07:05 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Do you know how to handle getSource() in ActionEvent.

Code:
evt.getSource()
The above line is quite similar to your code.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-09-2008, 07:23 AM
Member
 
Join Date: Jul 2008
Posts: 4
behrk2 is on a distinguished road
Thanks for your help, with that information, I came up with this...

Code:
private void gboard_row1_btn1ActionPerformed(java.awt.event.ActionEvent evt) { Object source = evt.getSource(); Color color = getBackground(); if (source == new Color(255, 255, 255)) color = Color.red; else if (source == Color.red) color = Color.orange; else if (source == Color.orange) color = Color.yellow; else if (source == Color.yellow) color = Color.green; else if (source == Color.green) color = Color.blue; else if (source == Color.blue) color = new Color(138, 43, 226); else if (source == new Color(138, 43, 226)) color = Color.red; setBackground(color); repaint(); }
But that still does not work. Any suggestions? I will keep trying to work with it and post if I come up with a solution
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-09-2008, 07:57 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
You comes in wrong way. Handle the getSource() in wrong way.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-09-2008, 08:03 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Here is a suggestion. randomly find a color and repaint the container with that selected color. Just think what happened in your loop. After action event is fired your loop executed. So...

And also you can do it in a thread. Set a color, and repaint. After certain time interval repeat the process.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-09-2008, 02:25 PM
Member
 
Join Date: Jul 2008
Posts: 1
Michael Dunn is on a distinguished road
why would you need a loop?

you (seemingly) have an actionListener tied to a button, so all you
need to do is have class fields for the color array and the counter,
then in gboard_row1_btn1ActionPerformed you have

gboard_row1_btn1.setBackground(colors[i % colors.length]);
i++;
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-09-2008, 06:54 PM
Member
 
Join Date: Jul 2008
Posts: 4
behrk2 is on a distinguished road
Thanks Michael,

That looks like a much better way. I got the same answer in another forum I had posted to as well...

Code:
A far better way to use the index is with the % operator. . . . colors[index++ % colors.length] . . . You can reduce the entire method to code: -------------------------------------------------------------------------------- public void actionPerformed(ActionEvent e){ m_btn1.setBackground(colors[index++ % colors.length]); } -------------------------------------------------------------------------------- You might have problems when you have clicked 2147483647 times with arithmetical overflow, however. Try index++ and ++index, see which works better. You can divide it into 3 statements if you prefer: index++; index %= colors.length; myButton.setBackground(colors[index]); And colors is identical for all instances, so it could beneficially be a static field.
I will try it out!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
when muse pressed the background change pcman Java Applets 1 03-18-2008 01:51 AM
window background color? javan00b New To Java 3 01-29-2008 12:43 PM
JButton onClick? Joe2003 AWT / Swing 2 01-06-2008 05:04 PM
How to change TXT color Onclick dave700800 New To Java 1 12-08-2007 03:39 AM
How to change shape of JButton FaRuK AWT / Swing 1 05-19-2007 02:56 PM


All times are GMT +3. The time now is 03:24 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org