1

I want execute ExampleSwingWorker1 from Main GUI. Main GUI class compile and doing some jFrame and DB operations and show main app screen to user. And I have another class for store all of my Swingworkers.

public class WorkerClass {
  public class ExampleSwingWorker1 extends SwingWorker<Void, Void> {      
        protected Void doInBackground() throws Exception {
            process1();
            process2();
            process3();
            process4();
            process5();
            process6();
            return null;
        }
        public void done() {
           Toolkit.getDefaultToolkit().beep();
        }
    }

}

Button Action in MainGui Class;

private void buttonRefreshActionPerformed(java.awt.event.ActionEvent evt) {                                              

  WorkerClass.ExampleSwingWorker1 trying = new WorkerClass.ExampleSwingWorker1();   
} 

I tried with the above methods for instantiating ExampleSwingWorker1 but it's not possible. But this Oracle Link offer this method for reach inner class.

fzzle
  • 1,466
  • 5
  • 23
  • 28
Black White
  • 700
  • 3
  • 11
  • 31

1 Answers1

1

You need an instance of WorkerClass first

Workerclass worker = new WorkerClass();
WorkerClass.ExampleSwingWorker1 trying = worker.new ExampleSwingWorker1();
trying.execute();

Read more: Inner classes|Nested classes

Note: If it doesn't use WorkerClass instance method's think it to make it static then you don't need an instance of WorkerClass to create a ExampleSwingWorker1 instance.

Note2: It's recommended that you add @Override annotation. Reasons? Read here

Community
  • 1
  • 1
nachokk
  • 14,363
  • 4
  • 24
  • 53
  • Thanks for the comments. Below code working for me. But i have some logical problem in Swingworker i think. Because i reference MainGUI class from there. – Black White Mar 21 '14 at 15:47
  • Then you have a bad design i guess.. MainGUI use WorkerClass and WorkerClass use MainGUI , they are too coupled – nachokk Mar 21 '14 at 15:52
  • Yes. I must find a way using MainGUI methods and update MainGUI components from this SW without instance it. – Black White Mar 21 '14 at 16:09
  • @BlackWhite 1) You can pass the reference by parameter `new ExampleSwingWorker(this)` 2) you can create your own `WorkerListener` then when `trying.addPropertyChangeListener(new MyListener)` where MyListener can/should be an inner class of `MainGUI` then there you can update. :D 3) Another possibility is using a kind of mediator that communicates between WorkerClass and MainGUI – nachokk Mar 21 '14 at 16:20