0

Can it be done? I tried doing this, but it gives a compilation error:

Timer t = new Timer(1000,new ActionListener() {
    public void actionPerformed(ActionEvent event) {

    }
});

Here's the entire code for reference


Full Code:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Timer;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class Scratch {


    public static void main(String[] args) {
        JFrame frame = new JFrame("Moving Rectangle");
        frame.setSize(1000,700);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JComponent() {
            public void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;

            }

        });
        Timer t = new Timer(1000,new ActionListener() {
            public void actionPerformed(ActionEvent event) {

            }
        });

    }
}

I need to type something as my question is mostly code.

5
  • what is the compilation error ? Commented May 2, 2015 at 22:07
  • Yes, it can be done quite simply, and you almost got it! Commented May 2, 2015 at 22:09
  • It says that the constructor is undefined. :( Commented May 2, 2015 at 22:16
  • It says that the constructor is undefined - makes no sense to me because I copied and pasted the code and it compiled fine. Post a proper SSCCE that demonstrates the problem. You muse have other problems, maybe a missing import statement. So basically create a main() method and add the code to the method and get to compile. Commented May 2, 2015 at 22:20
  • Ok, here's my entire code: Commented May 2, 2015 at 22:23

1 Answer 1

1

but it gives a compilation error:

When you ask a question post the error so we don't have to guess.

When I added your code to an empty main() method I got the following because I had a lot of standard import statements in the test class:

Main.java:21: error: reference to Timer is ambiguous, both class java.util.Timer in java.util and class javax.swing.Timer in javax.swing match
Timer t = new Timer(1000, new ActionListener()

The solution can be to use:

javax.swing.Timer t = new javax.swing.Timer(1000, new ActionListener()

to avoid confusion.

Edit:

Did you look at my solution above? Notice how I'm using a javax.swing.Timer?

import java.util.Timer;

Don't use java.util.Timer. With Swing you need to use the Swing Timer, so the code is executed on the EDT.

Instead use:

import javax.swing.Timer;
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry, the actual error is that it says that the constructor is undefined. :(
@KaneWilliamson, see the edit. This is why we need the SSCCE. To understand the context of your code and the compiler message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.