Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Swing timer not stopping/starting...

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 >_<.


    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();
            }
        }
    Last edited by hypnotoad; October 19th, 2012 at 01:44 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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:
    	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)

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default 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:

        class StopUpdating implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                System.out.println("updateTimer is null? " + (updateTimer == null));
                updateTimer.stop();
            }
        }

Similar Threads

  1. Starting and Stopping Tomcat7 Server from Java Program
    By srinivasgoudn in forum Java Theory & Questions
    Replies: 4
    Last Post: December 26th, 2011, 11:03 PM
  2. Timer implementation in swing
    By portem1 in forum Algorithms & Recursion
    Replies: 1
    Last Post: January 19th, 2011, 08:14 AM
  3. Anyone familiar with the swing timer?
    By BigJoe in forum AWT / Java Swing
    Replies: 5
    Last Post: December 28th, 2010, 12:15 PM
  4. Starting and Stopping a loop with JButtons
    By Endevor in forum What's Wrong With My Code?
    Replies: 8
    Last Post: December 8th, 2010, 03:37 PM
  5. Help - Swing Timer, 2 KeyEvents
    By Gheta in forum AWT / Java Swing
    Replies: 2
    Last Post: July 29th, 2009, 02:46 PM