简体   繁体   中英

How to keep a method running continuously in background?

I want that my pdf chek method runs in the background but I really have no idea how I can implement my method into an SwingBackgroupWorker or Thread...

public class PDFCheck extends JPanel {

    private void testAllFontsAreEmbedded(PDFDocument pdf) throws PDFDocumentException {
        for (PDFFont font : pdf.listFonts()) {
            if (!font.isEmbedded()) {
              this.problems.add(new ProblemDescription<PDFDocument>(pdf, "font not embedded: " + font.getName()));
            }
        }
        }
}

Thank you very much...

I tried this code...but it doesn't seem to work..

public static class SwingBackgroupWorker extends SwingWorker<Object, Object> {

        @Override
        protected Object doInBackground() throws Exception {
            private void testAllFontsAreEmbedded(PDFDocument pdf) throws PDFDocumentException {
                for (PDFFont font : pdf.listFonts()) {
                    if (!font.isEmbedded()) {
                      this.problems.add(new ProblemDescription<PDFDocument>(pdf, "font not embedded: " + font.getName()));
                    }
                }
                }
        }

I would then start the backgroundworker with new SwingBackgroupWorker().execute();

    }

How can I run the Backgroundworker to test it?

 public class MoveIcon extends JPanel {

        public class MyTask extends SwingWorker<Void, Void> {

            @Override
            protected Void doInBackground() throws Exception {
                int i = 0;

                while (i < 10) {
                    System.out.print(i);
                    i++;
                }
                return null;
            }
        }

    public static void main(String[] args) {

        new MyTask();

    }
}

That doesn't work :(

I usually create inner classes for SwingWorker s. So you could put your SwingWorker in a private inner class of PDFCheck and add the fields (in your case just the pdf ) you need to access inside your worker. You then can set them through the constructor. You could do something like this:

public class PDFCheck extends JPanel {

/* ... */

    private class MyTask extends SwingWorker<Void, Void> {

        PDFDocument pdf;

        MyTask(PDFDocument pdf)
        {
            this.pdf = pdf;
        }

        @Override
        protected Void doInBackground() throws Exception
        {
            for (PDFFont font : pdf.listFonts()) 
            {
                if (!font.isEmbedded()) 
                {
                    PDFCheck.this.problems.add(new ProblemDescription<PDFDocument>(pdf, "font not embedded: " + font.getName()));
                }
            }
        }
    }

/* ... */

    // Call the Swing Worker from outside the class through this method
    public void runWorker()
    {
         MyTask task = new MyTask(pdfFile);
         task.execute()
    }

}

Call it then from inside the PDFCheck class like this:

MyTask task = new MyTask(pdf);
task.execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM