I’m trying to make a Swing Timer that is executes when I press a button but never starts.
My code is:
public class prueba extends JFrame {
java.util.Date date= new java.util.Date();
private JPanel contentPane;
Timer timero;
........
public prueba() {
..........
timero = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("otra vez....uauuauauaua");
}
});
btnIniciar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
timero.start();
...............
Could you help me please?
Thanks and sorry for my english!
When I do these I have an error:
public class prueba extends JFrame {
java.util.Date date= new java.util.Date();
private JPanel contentPane;
Timer timero;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
prueba frame = new prueba();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public prueba() {
final WebManager web = new WebManager("user","pass","http://www.spanishtracker.com/login.php");
//web.EnableDebug();
web.doLogin();
web.getCookies;
.....
contentPane.add(txtSalida, gbc_txtSalida);
timero.start();
it gives me an error:
java.lang.NullPointerException
at prueba.<init>(prueba.java:216)
at prueba$1.run(prueba.java:56)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
These lines are:
216 timero.start();
56 prueba frame = new prueba();
You may have another
Timernamedtimerothat shadows the one on which you call.start(). For reference, this example shows how to start and stop a timer, and this example shows how to expose astart()method that forwards to an enclosed timer. The latter is useful to avoid starting the timer prematurely, before the constructor concludes.