Swing timer not stopping/starting...
Hi, the code below is my attempt at a timer to update data pulled from a database. It's not working and I'm very new to java. Does anyone know why? The start/stop buttons are JMenuItems... Thansk >_<.
Code :
public class DBMonitor extends javax.swing.JFrame {
public final static int SECOND = 1000;
private Timer updateTimer;
public DBMonitor() {
initComponents();
StartButton.addActionListener(new StartUpdating());
StopButton.addActionListener(new StopUpdating());
Timer updateTimer = new Timer(10*SECOND, new ActionListener() {
public void actionPerformed(ActionEvent e) {
UpdateAll();
Date updateTime = new Date();
timer_Label.setText(updateTime.toString());
System.out.println("Is this working?!");
}
});
updateTimer.setInitialDelay(5*ONE_SECOND);
//updateTimer.start();
}
class StartUpdating implements ActionListener {
public void actionPerformed(ActionEvent e) {
updateTimer.start();
}
}
class StopUpdating implements ActionListener {
public void actionPerformed(ActionEvent e) {
updateTimer.stop();
}
}
Re: Swing timer not stopping/starting...
What exactly do you mean when you say it doesn't work? Does it throw an Exception? Does it have a strange behavior?
Hint: What is the value of updateTimer? Are you sure?
Re: Swing timer not stopping/starting...
Crap. It was not throwing an error to the screen, but in the java output screen it was. Also, in my main post, I meant to have the line "updateTimer.start();" commented out. Is it possible though, to have the timer start running when the program first starts and then still use the start stop buttons? I don't follow your hint about the updateTimer value :(...
Start Button:
Code :
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at LabMonitor$StartAction.actionPerformed(LabMonitor.java:53)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.menuDragMouseReleased(BasicMenuItemUI.java:943)
at javax.swing.JMenuItem.fireMenuDragMouseReleased(JMenuItem.java:585)
at javax.swing.JMenuItem.processMenuDragMouseEvent(JMenuItem.java:482)
at javax.swing.JMenuItem.processMouseEvent(JMenuItem.java:428)
at javax.swing.MenuSelectionManager.processMouseEvent(MenuSelectionManager.java:322)
at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.eventDispatched(BasicPopupMenuUI.java:869)
at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Toolkit.java:2430)
at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Toolkit.java:2322)
at java.awt.Toolkit.notifyAWTEventListeners(Toolkit.java:2280)
at java.awt.Component.dispatchEventImpl(Component.java:4757)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Stop Button:
Code :
at LabMonitor$StopAction.actionPerformed(LabMonitor.java:60)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.menuDragMouseReleased(BasicMenuItemUI.java:943)
at javax.swing.JMenuItem.fireMenuDragMouseReleased(JMenuItem.java:585)
at javax.swing.JMenuItem.processMenuDragMouseEvent(JMenuItem.java:482)
at javax.swing.JMenuItem.processMouseEvent(JMenuItem.java:428)
at javax.swing.MenuSelectionManager.processMouseEvent(MenuSelectionManager.java:322)
at javax.swing.plaf.basic.BasicPopupMenuUI$MouseGrabber.eventDispatched(BasicPopupMenuUI.java:869)
at java.awt.Toolkit$SelectiveAWTEventListener.eventDispatched(Toolkit.java:2430)
at java.awt.Toolkit$ToolkitEventMulticaster.eventDispatched(Toolkit.java:2322)
at java.awt.Toolkit.notifyAWTEventListeners(Toolkit.java:2280)
at java.awt.Component.dispatchEventImpl(Component.java:4757)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Re: Swing timer not stopping/starting...
His hint was about variable shadowing. Have you heard of this before? The value of your Timer variable, as you're finding out, is null because you're doing this. To test that this is so, simply add a println to your code:
Code java:
class StopUpdating implements ActionListener {
public void actionPerformed(ActionEvent e) {
System.out.println("updateTimer is null? " + (updateTimer == null));
updateTimer.stop();
}
}