Piyush Yawalkar
Piyush Yawalkar

Reputation: 264

I want to do some task in Background in swing

I am using swing timer to to load different pdf files in swing application but I am facing a problem when ever I execute a program the screen remains blank for few seconds like 4 to 5 seconds and then the pdf file is rendered so during this time I want to show a message like please wait. Here is my sample code

          if (type[i].equalsIgnoreCase("PDF")) {
            int k = i;
            pdfTimer = new Timer(0, (ActionEvent e) -> {
                renderPDF(k);

            });
            pdfTimer.setDelay(1000*2);
            pdfTimer.start();

Upvotes: 0

Views: 3289

Answers (3)

Rahul
Rahul

Reputation: 299

Just an alternative for SwingWorker

By default show some message like Loading... and create a Thread to run in the background which loads the PDF and updates the window

class BackgroundThread implements Runnable {

   @Override
   public void run() {

      // the Swing call below must be queued onto the Swing event thread
      SwingUtilities.invokeLater(new Runnable(){
         @Override
         public void run() {
            // OK To make Swing method calls here
            loadFile(args..);
            repaint();//Or something similar that fits your purpose
         }
      });
   }
}

Upvotes: 0

Erwan C.
Erwan C.

Reputation: 719

You can display a progress bar and compute the renderding on a second thread.

JLabel lblWait = new JLabel("Please wait...");
    lblWait .setBounds(116, 26, 113, 14);
    contentPanel.add(lblWait );

    final JProgressBar progressBar = new JProgressBar();
    progressBar.setStringPainted(true);
    progressBar.setBounds(72, 66, 187, 16);
    contentPanel.add(progressBar);
    {
        JPanel buttonPane = new JPanel();
        buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
        getContentPane().add(buttonPane, BorderLayout.SOUTH);
        {
            final JButton btFin = new JButton("Cancel");
            btFin.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    dispose();
                }
            });

            buttonPane.add(btFin);
        }
    }
     Thread t = new Thread() {
            public void run() {
              //You launch the thread with your progress bar as an argument
                maStructure.setaAfficher(new MgtSimulation(aAfficher, nombreSimulations).jouerSimulations(progressBar));
                maStructure.actualiserAffichage();
                dispose();
            }
          };
          t.start();
}

And you change your progress bar value in your method

public  BeanAffichage jouerSimulations(JProgressBar progressBar){
    //Variables

     for (int i = 0; i < nombreSimulations; i++) {           
        //Computing

         progressBar.setValue(Whatever you want);
    }



    return aAfficher;
}

Upvotes: -1

Branislav Lazic
Branislav Lazic

Reputation: 14816

Run rendering on SwingWorker's background thread (doInBackground) method. That way, your GUI will remain responsive. From done method you can notify user that rendering is done. Keep in mind not to update any Swing GUI from doInBackground method since it runs outside of EDT.

P.S. Swing Timer is for repetitive tasks.

Upvotes: 3

Related Questions