Java multithreading - SwingWorker

In a previous articles, I've concerned about BackgroundWorker component of C# which implements multithreading technique. In this article, I'll show SwingWorker component of Java which also implements that .


Swingworker Class :

package swingworker;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.util.List;
/**
 *
 * @author chien
 */
public class Swingworker {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        // TODO code application logic here
           SwingUtilities.invokeLater(new Runnable() {
      @Override
      public void run() {
    final   JLabel jlbempty = new JLabel("http://proexper.blogspot.com");

        final JFrame frame = new JFrame();
        frame.setTitle("Test SwingWorker");
        frame.setSize(600, 400);
        frame.setLayout(new GridLayout(3, 3));
        



        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
                jlbempty.addMouseListener
                (
                        new MouseAdapter()
                        {
                            public void mouseClicked(MouseEvent e)
                            {
                             MyBlankWorker a=new MyBlankWorker(jlbempty);
                             a.execute();
                            }
                        }
                );
              
                jlbempty.setSize(50, 20);
                jlbempty.setBorder(BorderFactory.createLineBorder(Color.yellow));
                     frame.add(new JLabel("Click the label below to run Swingworker. The progress percentage will show during the swingworker running"));
        frame.getContentPane().add(jlbempty, BorderLayout.CENTER);
                frame.setVisible(true);
         
        frame.setVisible(true);
      }
    });
  
  
    }
   
}


MyBlankWorker  Class:
package swingworker;

import java.util.List;
import javax.swing.*;
/**
 *
 * @author chien
 */
public class MyBlankWorker extends SwingWorker<Integer,String> {
   
    private final JLabel fLabel;
    public MyBlankWorker( JLabel aLabel ) {
      fLabel = aLabel;
    }
   
     @Override
  protected Integer doInBackground() throws Exception {
    // Start
   
    for(int i=0; i<=100; i++)
    {
    Thread.sleep(1000);
    publish("Running");
    setProgress(i);
    }
   

    // Complete
    publish("Complete");
    return 1;
  }
 
  @Override
  protected void process(List<String> chunks) {
    // Messages received from the doInBackground() (when invoking the publish() method)
      fLabel.setText(String.valueOf(getProgress())+"% completed");
     
  }
}
Share on Google Plus

About Chien

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.

0 comments:

Post a Comment