motaa
motaa

Reputation: 337

Unexplainable behaviour of setProgress in SwingWorker

I did a quick and dirty implementation of SwingWorker to test the publish() and setProgress() methods. When I set the parameter for the setProgress() method like stated in the complete sourcecode below, everything works as expected. However if I set the setProgress parameter like this: setProgress((int) ((i/2000) * 100)); OR like this setProgress((int) (i * (100/2000)));

The progressbar does not update since the propertyChange is not getting fired.

I seriously have no clue why this is since in my opinion I don't see anything mathematically wrong there...

Any ideas?

Thanks in advance!

Best regards

package test;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.SwingWorker;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

public class ConsoleTest extends SwingWorker<Void, String>{ 

private JTextPane console;
private JProgressBar pb;

public ConsoleTest(JProgressBar pb, JTextPane out){

    this.console = out;
    this.pb = pb;
}

private static void createGUI(){

    JFrame container = new JFrame();
    container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JTextPane console = new JTextPane();
    JButton start = new JButton("start");
    JButton reset = new JButton("reset");
    JPanel btnpane = new JPanel();
    btnpane.add(start);
    btnpane.add(reset);
    btnpane.add(new JCheckBox( "Click me to proof UI is responsive" ));
    JProgressBar pb = new JProgressBar(0,100);
    btnpane.add(pb);
    container.add(btnpane, BorderLayout.SOUTH);

    JScrollPane sp = new JScrollPane(console);
    sp.setPreferredSize(new Dimension(800,800));
    container.add(sp,BorderLayout.NORTH);

    start.addActionListener(e -> { ConsoleTest test = new ConsoleTest(pb, console);
                                   test.addPropertyChangeListener(evt -> { if ("progress".equals(evt.getPropertyName())) {
                                                                             System.out.println("check");
                                                                             test.getPG().setValue((int)evt.getNewValue());
                                                                           }                                                                                  
                                                                         });
                                   test.execute();
                                 });
    reset.addActionListener(e -> clear(console));
    container.pack();
    container.setVisible(true);


}

private static void clear(JTextPane console) {

    console.setText("");
}



public JProgressBar getPG(){

    return this.pb;
}

@Override
protected Void doInBackground() throws Exception {

    LinkedList<Integer> list = new LinkedList<>();
    for (int i = 0; i< 2000;i++){
        list.add(i);
        if(((i+1) % 5) == 0)
            publish(Integer.toString(i+1));
        setProgress((int) (i*100/2000));
        Thread.sleep(1);            
    }
    return null;
}

@Override
protected void process(List<String> chunks){

        StyledDocument doc = this.console.getStyledDocument();
        try {
            for(String s: chunks)
            doc.insertString(doc.getLength(),"- "+s+" elements added to the list\n", null);
        } catch (BadLocationException e) {
            e.printStackTrace();
        }       
}


@Override
protected void done() {

    try {
        System.out.println("DONE!!!");
    } 
    catch (Exception ignore) {
    }
}

public static void main(String[] args) {

    createGUI();
}

}

Upvotes: 1

Views: 255

Answers (2)

ArcticLord
ArcticLord

Reputation: 4039

you work with integers !!

setProgress((int) ((i/2000)*100));

is always 0 since (int)i / 2000 is always 0

same problem with

setProgress((int) (i*(100/2000)));

100 / 2000 is always 0 in integer

try

setProgress((int) (((float)i/2000)*100));

or

setProgress((int) (i*((float)100/2000)));

and it will work

Upvotes: 3

whyn0t
whyn0t

Reputation: 301

you should make (i * 100)/2000 the formula is

(current_state * 100) / final_state

Upvotes: 0

Related Questions