1

I want to display a JProgressBar in the foreground and run a task in background, but my program does not start the progress bar simply.

import java.awt.EventQueue;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;
import javax.swing.JProgressBar;


public class sw {

private JFrame frame;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                sw window = new sw();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the application.
 * @throws InterruptedException 
 */
public sw() throws InterruptedException {
    initialize();
}

/**
 * Initialize the contents of the frame.
 * @throws InterruptedException 
 */
private void initialize() throws InterruptedException {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    JProgressBar progressBar = new JProgressBar();
    progressBar.setIndeterminate(true);
    progressBar.setBounds(96, 91, 146, 14);
    frame.getContentPane().add(progressBar);
    progressBar.setVisible(true);
    for(int i=0;i<5;i++)
    {
        System.out.println(i);
        TimeUnit.SECONDS.sleep(1);
    }
    progressBar.setVisible(false);
   }
   }

How to go about it?

sam
  • 59
  • 1
  • 1
  • 7
  • 1
    Don't block the EDT (Event Dispatch Thread). The GUI will 'freeze' when that happens. See [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for details and the fix. BTW - it's interesting you added the tag for the most common way to solve this problem (a `SwingWorker`), but there's no mention of it in the code. Why not? For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Jan 12 '17 at 10:31
  • Well, i know the few ways of implementing it, but they are quite lengthy and i am unable to understand the examples as they usually perform some specific task. – sam Jan 12 '17 at 10:36
  • I am including the entire code, if that helps. Swing Worker is pretty complex for me i guess. – sam Jan 12 '17 at 10:38
  • OK.. let's revisit *"How to go about it(?)"" - 1st step, try implementing the way you think is should work. Try and implement a `SwingWorker` for your long running code. Get back to us when you strike a *particular* problem. SO is neither a tutoring site, nor a code generation service. Voting to close as 'too broad'. – Andrew Thompson Jan 12 '17 at 10:44
  • ok fine. give me a day's time – sam Jan 12 '17 at 10:47
  • 1
    As an [example](http://stackoverflow.com/questions/32807495/how-to-return-value-from-swingworker-class-and-use-in-other-class-and-enable-men/32819321#32819321) of some of things Andrew's been mentioning – MadProgrammer Jan 12 '17 at 11:09

0 Answers0