×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Himesh Sharma
Added: Feb 10, 2021 2:12 PM
Views: 4738
Tags: swingworker
  1.  static class Task_StringUpdate extends SwingWorker<Void, String> {
  2.  
  3.    JLabel jlabel;
  4.    public Task_StringUpdate(JLabel jlabel) {
  5.        this.jlabel = jlabel;
  6.    }
  7.  
  8.    @Override
  9.    protected void process(List<String> chunks) {
  10.        jlabel.setText(chunks.get(chunks.size()-1)); // The last value in this array is all we care about.
  11.        System.out.println(chunks.get(chunks.size()-1));
  12.    }
  13.  
  14.    @Override
  15.    protected Void doInBackground() throws Exception {
  16.  
  17.        publish("Loading Step 1...");
  18.        Thread.sleep(1000);
  19.        publish("Loading Step 2...");
  20.        Thread.sleep(1000);
  21.        publish("Loading Step 3...");
  22.        Thread.sleep(1000);
  23.        publish("Loading Step 4...");
  24.        Thread.sleep(1000);
  25.  
  26.        return null;
  27.    }
  28.  
  29.    @Override
  30.    protected void done() {
  31.        try {
  32.            get();
  33.            JOptionPane.showMessageDialog(jlabel.getParent(), "Success", "Success", JOptionPane.INFORMATION_MESSAGE);
  34.        } catch (ExecutionException | InterruptedException e) {
  35.            e.printStackTrace();
  36.        }
  37.    }
  38. }