Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

java - Using SwingWorker to add a progress bar in a GUI

I use SwingWorker to make a progress bar with Java Swing APIs.

I have a class that extends SwingWorker

    class Swinger extends SwingWorker {
private ClassAnalyzer classAnalyzer;

public Swinger(ClassAnalyzer classAnalyzer){
     this.classAnalyzer = classAnalyzer;
}
        @Override
        public Void doInBackground() throws InterruptedException {

            try
        {     
            int progress = 0;
            while (progress < 100) {

 // at this point I make certain elaboration on classAnalyzer                 

                progress++;

                //Call the process method to update the GUI
                publish(progress);

            }                       
        }
        catch(InterruptedException e)
        {
        }
        return null;
    }

    @Override
    protected void process(List chunks) {
     for (Integer chunk : chunks) {
        progressBar.setValue(chunk);

        //if the switchtype checkbox is selected then
        //change the progressbar to a determined type
        //once the progress has reached 50
        if (chunk > 49)
        {
            if (switchType.isEnabled() && switchType.isSelected())
            {
                progressBar.setStringPainted(true);

            }

        }
     }
 }

}

and a second class (I'm writing a piece of this)

 public Tester()
{
    JFrame guiFrame = new JFrame();

    //make sure the program exits when the frame closes
    guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    guiFrame.setTitle("Creating a Table Example");
    guiFrame.setSize(700,200);

    //This will center the JFrame in the middle of the screen
    guiFrame.setLocationRelativeTo(null);


    goButton = new JButton("Go");
    goButton.setActionCommand("Go");
    goButton.addActionListener(new ActionListener()
    {

        //When the button is clicked the SwingWorker class is executed and
        //the button is disabled
        @Override
        public void actionPerformed(ActionEvent event)
        {

            progressBar.setStringPainted(progressType.isSelected());
            ClassAnalyzer c = new ClassAnalyzer();
            Swinger task = new Swinger(c);
            task.execute();

            int methods = c.getNumberOfMethods();

            if(methods == 0){
            JOptionPane.showMessageDialogo(null, "methods not found");
            }

            goButton.setEnabled(false);
        }
    });

    }

When I launch in a Tester the second class, the message "methods not found" is displayed before progress bar appears, while I would like message appears in case after. What to do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The SwingWorker class also defines a done method where you can define what to do once your task finishes.

class Swinger extends SwingWorker {

    // all the rest of your code ^^

    @Override
    protected void done() {
        if(this.classAnalyzer.getNumberOfMethods() == 0)
            JOptionPane.showMessageDialog(null, "methods not found");            
    }
}

You don't have to worry about threading issues since this method is called on the EDT too.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...