aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
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
Author

Need Help with Coloring buttons on Clock

Tonia Billiot
Greenhorn

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();
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 ]

Tonia
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="http://www.coherentrambling.blogspot.com" target="_blank" rel="nofollow">http://www.coherentrambling.blogspot.com</a>
Tonia Billiot
Greenhorn

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(Color.red), 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

http://www.coderanch.com/t/340515/GUI/java/Threads-GUI
Tonia Billiot
Greenhorn

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

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
 
Consider Paul's rocket mass heater.
 
subject: Need Help with Coloring buttons on Clock