Results 1 to 7 of 7
- 07-09-2008, 04:52 AM #1
Member
- Join Date
- Jul 2008
- Posts
- 4
- Rep Power
- 0
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:
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?Java 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++; } }
Thanks!
- 07-09-2008, 05:05 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Do you know how to handle getSource() in ActionEvent.
The above line is quite similar to your code.Java Code:evt.getSource()
- 07-09-2008, 05:23 AM #3
Member
- Join Date
- Jul 2008
- Posts
- 4
- Rep Power
- 0
Thanks for your help, with that information, I came up with this...
But that still does not work. Any suggestions? I will keep trying to work with it and post if I come up with a solutionJava 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(); }
- 07-09-2008, 05:57 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
You comes in wrong way. Handle the getSource() in wrong way.
- 07-09-2008, 06:03 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 07-09-2008, 12:25 PM #6
Member
- Join Date
- Jul 2008
- Posts
- 62
- Rep Power
- 0
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, 04:54 PM #7
Member
- Join Date
- Jul 2008
- Posts
- 4
- Rep Power
- 0
Thanks Michael,
That looks like a much better way. I got the same answer in another forum I had posted to as well...
I will try it out!Java 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.
Similar Threads
-
when muse pressed the background change
By pcman in forum Java AppletsReplies: 1Last Post: 03-17-2008, 11:51 PM -
window background color?
By javan00b in forum New To JavaReplies: 3Last Post: 01-29-2008, 10:43 AM -
JButton onClick?
By Joe2003 in forum AWT / SwingReplies: 2Last Post: 01-06-2008, 03:04 PM -
How to change TXT color Onclick
By dave700800 in forum New To JavaReplies: 1Last Post: 12-08-2007, 01:39 AM -
How to change shape of JButton
By FaRuK in forum AWT / SwingReplies: 1Last Post: 05-19-2007, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks