Swing Timers

I an a novice Java programmer. I have a JApplet class that utilizes Swing. I seen the example code for Swing timers. I seen the chapter on Swing timers. I inserted some test code into my class and it will not accept the parameters for the Timer.

here is the sample code:

int delay = 1000; //milliseconds

ActionListener taskPerformer = new ActionListener() {

public void actionPerformed(ActionEvent evt) {

//...Perform a task...

}

};

new Timer(delay, taskPerformer).start();

The compiler says it can not accept the two parameters, delay and taskPerformer. It wants an empty parameter body. I can create a class in another file that works. Why does it not work in my JApplet. Any Ideas? I have tried it by taking out the extends JApplet and converting it to a Java application. I am going to use a timer to poll a network connection for data. I know there are two kinds of Timers, but since I am using Swing widgets I thought I was supposed to use Swing Timers.

[1026 byte] By [wwwlouiea] at [2007-11-27 0:50:46]
# 1
Hi, Have you checked your imports ?You must import javax.swing.Timer and not java.util.Timer.Olivier
olivieroa at 2007-7-11 23:21:13 > top of Java-index,Core,Core APIs...
# 2

I appreciate you help Olivier, I realize I need the javax.swing.Timer import. I have it and it still doesn't work.

Here are all my imports:

import com.sun.media.sound.Toolkit;

import javax.help.*;

import java.util.Date;

import java.awt.event.*;

import javax.swing.*;

import java.applet.Applet;

import java.applet.AppletContext;

import java.awt.*;

import java.awt.event.*;

import java.net.MalformedURLException;

import java.net.URL;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Timer;

import javax.swing.BorderFactory;

import javax.swing.BoxLayout;

import javax.swing.JApplet;

import javax.swing.JButton;

import javax.swing.JDesktopPane;

import javax.swing.JDialog;

import javax.swing.JFrame;

import javax.swing.JInternalFrame;

import javax.swing.JLabel;

import javax.swing.JMenu;

import javax.swing.JMenuBar;

import javax.swing.JMenuItem;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JProgressBar;

import javax.swing.JScrollPane;

import javax.swing.JTabbedPane;

import javax.swing.JTextField;

import javax.swing.JToggleButton;

import javax.swing.JToolBar;

import javax.swing.JWindow;

import javax.swing.KeyStroke;

import javax.swing.SwingConstants;

import javax.swing.border.BevelBorder;

import javax.swing.border.TitledBorder;

import org.eclipse.swt.widgets.Composite;

import javax.swing.ImageIcon;

import javax.swing.JCheckBox;

import javax.swing.JRadioButton;

import org.eclipse.swt.widgets.Canvas;

import java.awt.GridLayout;

import java.awt.Point;

import javax.swing.border.EtchedBorder;

import javax.swing.border.SoftBevelBorder;

import java.awt.SystemColor;

import java.awt.Dimension;

import javax.swing.JTextArea;

import java.awt.BorderLayout;

import javax.swing.JSplitPane;

import java.awt.GridBagLayout;

import java.awt.Rectangle;

import java.awt.Font;

import java.awt.GridBagConstraints;

import java.awt.Color;

I think my JApplet only lets me create "java.util.Timer'. timers objects. Since the constructor doesn't take any arguments. I wish I knew why. Like I stated earlier, I can create a Swing Timer class, and run them separately.

wwwlouiea at 2007-7-11 23:21:13 > top of Java-index,Core,Core APIs...
# 3

Actually, Olivier, I thought that import javax.swing.*; includes everything. I directly imported javax.swing.Timer; and the compiler says "import java.util.Timer; collides with another import statement. I think maybe if I get rid of the other timer import it will take the Swing timer. I will try it out and post if I am successful.

wwwlouiea at 2007-7-11 23:21:13 > top of Java-index,Core,Core APIs...
# 4
Oliviero , Good advice. It now takes the arguments. Doh! LOL
wwwlouiea at 2007-7-11 23:21:13 > top of Java-index,Core,Core APIs...