×

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:11 PM
Views: 4723
Tags: swingworker
  1. static class Task_IntegerUpdate extends SwingWorker<Void, Integer> {
  2.  
  3.    JProgressBar jpb;
  4.    int max;
  5.    JLabel label;
  6.    public Task_IntegerUpdate(JProgressBar jpb, int max, JLabel label) {
  7.        this.jpb = jpb;
  8.        this.max = max;
  9.        this.label = label;
  10.    }
  11.  
  12.    @Override
  13.    protected void process(List<Integer> chunks) {
  14.        int i = chunks.get(chunks.size()-1);
  15.        jpb.setValue(i); // The last value in this array is all we care about.
  16.        System.out.println(i);
  17.        label.setText("Loading " + i + " of " + max);
  18.    }
  19.  
  20.    @Override
  21.    protected Void doInBackground() throws Exception {
  22.        for(int i = 0; i < max; i++) {
  23.            Thread.sleep(10); // Illustrating long-running code.
  24.            publish(i);
  25.        }
  26.        return null;
  27.    }
  28.  
  29.    @Override
  30.    protected void done() {
  31.        try {
  32.            get();
  33.            JOptionPane.showMessageDialog(jpb.getParent(), "Success", "Success", JOptionPane.INFORMATION_MESSAGE);
  34.        } catch (ExecutionException | InterruptedException e) {
  35.            e.printStackTrace();
  36.        }
  37.    }
  38. }