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

07-09-2008, 06:52 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 4
|
|
|
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:
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!
|
|

07-09-2008, 07:05 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
Do you know how to handle getSource() in ActionEvent.
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.
|
|

07-09-2008, 07:23 AM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 4
|
|
Thanks for your help, with that information, I came up with this...
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
|
|

07-09-2008, 07:57 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
|
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.
|
|

07-09-2008, 08:03 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
|
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.
|
|

07-09-2008, 02:25 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 1
|
|
|
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++;
|
|

07-09-2008, 06:54 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 4
|
|
Thanks Michael,
That looks like a much better way. I got the same answer in another forum I had posted to as well...
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!
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|