File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Need Help with Coloring buttons on Clock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need Help with Coloring buttons on Clock" Watch "Need Help with Coloring buttons on Clock" New topic

Need Help with Coloring buttons on Clock

Tonia Billiot

Joined: Apr 14, 2005
Posts: 13
Believe it or not, I've come a long way w/ this clock; however, I cannot figure out how to color the buttons. When it is that certain hour, minute, & second, the buttons are supposed to be colored. I don't know how to color them. Any assistance w/ the following program is greatly appreciated.

import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.border.*;

public class NewClock extends JFrame implements ActionListener
public static void main(String[] args)
NewClock clock = new NewClock();

public NewClock()

public void myLayout()
int rowSize = 6;
int colSize = 2;
int horGap = 3;
int verGap = 3;
int hGap = 8;
int vGap = 4;
int numHrs = 12;
int numMin = 59;
int numSec = 59;
int hour;
int min;
int sec;

Panel pl1 = new Panel(); //create the frame
Panel pl2 = new Panel();
Panel pl3 = new Panel();
JFrame con = new myClock(100, 100, 600, 200); //container to hold all
BorderLayout conBL = new BorderLayout(hGap, vGap);

//Code that generates the Time
GregorianCalendar time = new GregorianCalendar();
hour = time.get(time.HOUR);
min = time.get(time.MINUTE);
sec = time.get(time.SECOND);
//Time Tester
System.out.print(hour + ":" + min + ":" + sec);

//generate the layout
GridLayout gl1 = new GridLayout(rowSize, colSize, horGap, verGap);
for (int i = 1; i <= numHrs; i++)
pl1.add(new Button(Integer.toString(i)) );

GridLayout gl2 = new GridLayout(rowSize, colSize, horGap, verGap);
for (int i = 1; i <= numMin; i++)
pl2.add(new Button(Integer.toString(i)) );

for (int i = 1; i <= numSec; i++)
pl3.add(new Button(Integer.toString(i)) );

Button b = new Button("Seconds");

con.getContentPane().add(pl1, BorderLayout.WEST);
con.getContentPane().add(pl2, BorderLayout.CENTER);
con.getContentPane().add(pl3, BorderLayout.EAST);
con.getContentPane().add(b, BorderLayout.SOUTH);
NewClock(int x, int y, int w, int h)
this.setBounds(x, y, w, h);
public void actionPerformed(ActionEvent e)


[ April 14, 2005: Message edited by: Tonia Billiot ]

Ashish Chopra
Ranch Hand

Joined: Nov 30, 2004
Posts: 134
Please specify what part of the button do you want to be colored?

If its the text on the button, it can be done by using the setForeground(Color x) API, and if its the color of the buttin itself then you can use setBackground(Color x) API.

hope this helps...

Quis Custodiet Ipsos Custodes<br /> <br />My blog: <a href="" target="_blank" rel="nofollow"></a>
Tonia Billiot

Joined: Apr 14, 2005
Posts: 13
I apologize for not being more specific. The first panel is for hours, the second panel is for minutes and the third for seconds. When it is 1 pm the one button on the hour panel is to be colored. When it is 06 minutes into the hour, that button is to be colored as well, and the same for seconds. I tried the setBackground(, and it did not work. I don't know what I am doing wrong. Thanks for your assistance.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
here's a recent thread which does what I think you're trying to do
Tonia Billiot

Joined: Apr 14, 2005
Posts: 13
Thanks for your assistance Michael
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Moving to Swing/AWT...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Need Help with Coloring buttons on Clock
jQuery in Action, 3rd edition