Java Timer Applet
I am not certain whether or not it’s taboo to post your code on the internet. There are several sites out there that do this, and I know without some code examples every now and then to look at, I might never understand certain elements.
It was hard to find any thing that closely related to what I’m about to post here. Basically, I was looking for some example that would illustrate for me how to use the Javax.swing.timer method.
I could understand that I needed to create the method by using:
timer = new Timer(1000, new TimerListener); which initializes a variable called timer as a new Timer method, counting by seconds, and called using the TimerListener class (inside class I had to create).
The reason why I was trying to create this java class was so I could imbed it into an applet for use on an html page.
This is the html code I used to call the applet:
<.html>
<.head><.title>Timer Applet<./timer><./head>
<.body><.applet code="timer.class" width="300" height="100"><./applet><./body><./html>
<./code>
Basically, the applet tag calls the java class and puts it in a predetermined sized window. When you load the html page in your browser (with Java enabled) it will call the java class, and run it.
So my Java code is as follows:
//Name: David Nick
//Date: 11.16.2009
//Purpose: To create a Java Timer Applet
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class timer extends JApplet
{
private JTextField tLabel;
private JPanel tPanel;
private JPanel bPanel;
private JButton startBtn;
private JButton stopBtn;
private Timer timer;
private int x = 0;
public void init()
{
timer = new Timer(1000, new TimerListener());
getContentPane().setBackground(Color.black);
//building the panels
buildTpanel();
buildBpanel();
//add panels to content pane
add(tPanel);
add(bPanel);
//setting the panel layout
setLayout(new GridLayout(2, 1));
}
private void buildTpanel()
{
//create the panel
tPanel = new JPanel();
//create Textfield that goes into the tPanel
tLabel = new JTextField("000000");
//add textfield to panel
tPanel.add(tLabel);
}
private void buildBpanel()
{
//create the panel
bPanel = new JPanel();
//create the buttons
startBtn = new JButton("Start");
stopBtn = new JButton("Stop");
//create the button listeners
startBtn.addActionListener(new StartListener());
stopBtn.addActionListener(new StopListener());
//add buttons to the panel
bPanel.add(startBtn);
bPanel.add(stopBtn);
}
//creating a listener for the start button
private class StartListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
timer.start();
}
}
//creating a listener for the stop button
private class StopListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
timer.stop();
}
}
//creating a timer listener
private class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
x++;
tLabel.setText("000000"+(x));
}
}
}
One of the difficulties for me was nailing down how to increment the counter once I hit the start button. The Timer method has only timer.start(); and timer.stop();. There is no timer.reset(); method. It is in poor taste to try and force the timer to set itself to all zeros because in clicking start it continues where you hit stop.
And so there you have the Java Timer Method used in an HTML applet.
