Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need Help with Coloring buttons on Clock

 
Tonia Billiot
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
clock.myLayout();
}

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);
con.getContentPane().setLayout(conBL);

//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);
pl1.setLayout(gl1);
for (int i = 1; i <= numHrs; i++)
{
pl1.add(new Button(Integer.toString(i)) );
}
pl1.validate();

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

pl3.setLayout(gl2);
for (int i = 1; i <= numSec; i++)
{
pl3.add(new Button(Integer.toString(i)) );
}
pl3.validate();

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);
con.validate();
con.pack();
}
NewClock(int x, int y, int w, int h)
{
super();
this.setBounds(x, y, w, h);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{

}

}
[ April 14, 2005: Message edited by: Tonia Billiot ]
 
Ashish Chopra
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Tonia Billiot
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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(Color.red), and it did not work. I don't know what I am doing wrong. Thanks for your assistance.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's a recent thread which does what I think you're trying to do

http://www.coderanch.com/t/340515/GUI/java/Threads-GUI
 
Tonia Billiot
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your assistance Michael
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing/AWT...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic