Results 1 to 6 of 6
  1. #1
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default Proper swing timer syntax

    I have been trying to get the proper syntax for the swing timer yet have not been able to get rid of all of the bugs that pop up. These are two ways that I have seen online. Netbeans has underlined the Timer constructor in both due to, as far as I can tell, incorrect type parameters, because Timer requires (int, ActionListener). If it is necessary to use an actionlistener, is there a way to have the timer start at the startup of the program?

    Note that I have put in 'hello world' instead of my code, but the bug remains the same.

    Java Code:
        private void timer() {
    //method 1
            Action hi = new AbstractAction() {
                public void actionPerformed(ActionEvent e) {
                    if (true) {
                        System.out.println("hello world");
                    }
                }
            };
            Timer t = new Timer(1000, hi).start();
    
    
    //method 2
            Timer timers = new Timer(1000, new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("hello world");
                }
            });
        }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    What bug?
    What error are you getting and where?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    Show your import statement.

    db

  4. #4
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    I have the entire program code below. I am trying to use the timer to animate the oval's movement across the JPanel. I doubt this is the issue, but it may be. I just get a 'cannot find symbol' explanation for the underlined Timer constructor, and my guess is that it is due to an incorrect Timer constructor:

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.BorderFactory;
    import javax.swing.JPanel;
    import java.util.Timer;
    import javax.swing.AbstractAction;
    import javax.swing.Action;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    class MyPanel extends JPanel {
    
        private int ovalx = 100;
        private int ovaly = 80;
    
        public MyPanel() {
            setBorder(BorderFactory.createLineBorder(Color.black));
        }
    
        private void timer() {
            //method 1
            Action hi = new AbstractAction() {
    
                public void actionPerformed(ActionEvent e) {
                    if (true) {
                        System.out.println("hello world");
                    }
                }
            };
            Timer t = new Timer(1000, hi).start();
    
            //method 2
            Timer timers = new Timer(1000, new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    System.out.println("hello world");
                }
            });
        }
    
        private void moveOval(int x, int y) throws InterruptedException {
            for (int i = 0; i < 5; i++) {
                if (ovalx != x || ovaly != y) {
                    repaint(ovalx, ovaly, 50, 50);
                    ovalx = x;
                    ovaly = y;
                    repaint(ovalx, ovaly, 50, 50);
                    Thread.sleep(200);
                }
                x = x + 30;
                y = y + 30;
            }
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillOval(ovalx, ovaly, 50, 50);
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
        private static void createAndShowGUI() {
            //System.out.println("Created GUI on EDT? "+ SwingUtilities.isEventDispatchThread());
            JFrame f = new JFrame("Swing Paint Demo");
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new MyPanel());
            f.pack();
            f.setVisible(true);
        }
    }

  5. #5
    snaquetime is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    I got it! You were right DarrylBurke. After copying, pasting, and finding bugs, I see that I imported java.awt.Timer, not javax.swing.Timer, and now it works. Thanks for your help!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default

    I see that I imported java.awt.Timer, not javax.swing.Timer
    Wrong again. You had imported java.util.Timer.

    db

Similar Threads

  1. help with Swing Timer animation
    By tomas1991 in forum New To Java
    Replies: 7
    Last Post: 03-19-2010, 10:06 AM
  2. Swing Timer Questions
    By morfasto in forum New To Java
    Replies: 9
    Last Post: 11-14-2009, 04:48 PM
  3. Timer on swing
    By finzaiko in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2009, 08:45 AM
  4. Timer in Swing app - refreshing label
    By qoqosz in forum New To Java
    Replies: 5
    Last Post: 03-09-2009, 03:17 AM
  5. [SOLVED] Swing Timer issue
    By Doctor Cactus in forum New To Java
    Replies: 6
    Last Post: 03-03-2009, 01:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •