This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Trying to make a button but don't know where to start Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Trying to make a button but don Watch "Trying to make a button but don New topic
Author

Trying to make a button but don't know where to start

Mack Grill
Greenhorn

Joined: Feb 27, 2012
Posts: 29


I'm trying to make a button that will change the color of the block when clicked.

Just need to know how to set the actionListener for the colors


here is the code that I'm working with:

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
class Box would need a color field
in box's display(), 'g' would be set to that color before drawing
actionListener would change the respective box's color to whatever, and call repaint()
Mack Grill
Greenhorn

Joined: Feb 27, 2012
Posts: 29
So, don't use actionListener for this class?

Also, when you're saying that class Box will need a color field, are you saying to change all color values and replace them with set.Color statements?

side note: Just noticed that I didn't submit this in beginning java so my apologizes for that
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> So, don't use actionListener for this class?

actionListener is needed. from previous reply
"actionListener would change the respective box's color to whatever, and call repaint()"

> Also, when you're saying that class Box will need a color field...

look at the constructor
new Box(0 * boxSize , 0*boxSize, boxSize, boxSize, new Color(0* 255/ gridSize, 0* 255/ gridSize , 0));

unless Box() is a JButton and new Color(0* 255/ gridSize, 0* 255/ gridSize , 0) is used to set its background in the constructor, a field would be needed.

> are you saying to change all color values and replace them with set.Color statements?

are you trying to change the color of a single box/button when clicked, or are all boxes/buttons to be the same color when one of them is clicked?
if a single box/button, then you would need a color field, so that display(), which is method of Box(), can set g's color prior to painting/drawing
e.g.
let's say you want to change the color of (Box) b1 to (could be random color, anything) Color.RED
in the actionListener (after determining source is b1, if needed) the code would go something like this
b1.setColor(Color.RED); //or b1.setColor(color);// where 'color' is set some other way
repaint();// or Grid.this.repaint();//depends on how its set up
so, from repaint() which calls paint(), in turn calls b1.display(g);
and in dislpay(g), would be
g.setColor(color); //just previously set to Color.RED
g.fill/draw/whatever;//and it would be done in Color.RED



 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Trying to make a button but don't know where to start
 
Similar Threads
Please need help with calculator program...
problem with handling the states of the events
GridLayout
javax.crypto.BadPaddingException for AES when encrypting and decrypting multiple times
Mouse shift event coding - to trigger a predetermined sequence of images to a GUI