Search code examples
javamultithreadingswtswingworker

Java SwingWorker while using SWT


I have a problem as follows:

I've written a simple minimalistic Application that utilizes the SWT for the GUI. In a specific tab, where it displays a table that get's filled with Informition via a REST Api Call. Additionaly, i have another method to export this table into a CSV-file. This works absolutely fine. Now I need some kind of autoupdate/-export for which I implemented a Swing-Worker like this:

    protected class AutoExportWorker extends SwingWorker<Integer, String> {


    @Override
    public Integer doInBackground() throws Exception {


        System.out.println("Worker Start!");

        while (true) {
            System.out.println("In Loop");

                            //updateTable();
                            //exportToCSV();

            for (int i = 0; i<interval;i++) {

                System.out.println(interval - i);
                Thread.sleep(1000);

            }
        }   
    }

The Class is a subclass of the Composite which views the table and holds the methods "updateTable()" and "exportsToCSV()". Now I'm aware that this Worker doesn't do much when I call these Methods but I can't figure out, how to do this correctly. Do you have any hints for me?

Best regards!


Solution

  • If you are using Eclipse RCP, you should use Jobs API. In plain SWT, there is no built-in solution. You need to use Display.asyncExec to update the table.