Search code examples
javaswingworker

SwingWorker: Syntax error on token "execute" -?


I'm trying to use a SwingWorker but for some reason at worker.execute(); I receive the following error:

"Syntax error on token "execute", Identifier expected after this token"

That doesn't help much, it's a generic error, and no matter how stupid it might be, it's been torturing me for hours.. I've seen that moving SwingWorker somewhere else outside the class may fix it, but I don't understand how and why and why it should not work like this! Ugh!

public class App {

    private App() {  // CONSTRUCTOR

        final int WINHSIZE = 2000;
        final int WINVSIZE = 2000;

        class Enjoy extends JPanel  {

            @Override
            public void paintComponent(Graphics g) {
                g.drawLine(0, 0, 2000, 2000);
            }

            class MyExecutor extends SwingWorker<Void,Void> { 

                @Override
                protected Void doInBackground() {
                    return null;
                }

                @Override
                protected void done(){ // runs on EDT

                }
            }

            MyExecutor worker = new MyExecutor();
            worker.execute(); // What the hell is going wrong here?

        }

        Runnable runner = new Runnable() { 
            @Override
            public void run() {
                JFrame f = new JFrame("Title");
                JPanel panel = new Enjoy();
                JScrollPane myScrollPane = new JScrollPane(panel);
                f.add("Center", myScrollPane);
                f.pack();
                f.setVisible(true);
            }
        };

        SwingUtilities.invokeLater(runner);

    }

    public static void main(String[] args) {
        new App();
    } 
}

Thanks.


Solution

  • worker.execute(); must be inside some method, it can't be directly within the scope of your Enjoy class.