0

I've been writing an ftp server app and i need to show an indeterminate progressbar on the screen.But it won't show. I've repainted the user interface,yet it won't show. the code is:

JProgressBar pb = new JProgressBar();
    pb.setIndeterminate(true);
    pb.setStringPainted(true);
    pb.setString("Transferring files...");
    pb.setVisible(true);
    pb.setBounds(0, 0, 600, 300);
    if (files.length > 0) {

        remove(panel);
        add(pb);
        this.revalidate();
        this.repaint();
    } else {
        JOptionPane.showMessageDialog(this, "Select files to send!", "ERROR!", JOptionPane.ERROR_MESSAGE);
        return;

    }
    try {
        for (File f : files) {
//send files code-Heavy code

    } catch (SocketException e) {
 //A JOptionPane exists here
        e.printStackTrace();

    } catch (Exception e) {
 //A JOptionPane exists here
        e.printStackTrace();
    }
    remove(pb);
    add(panel);
    statt.setText("Not connected");
    server.setEnabled(true);
    send.setEnabled(false);
    ss.setText("Not Selected");
    this.revalidate();
    this.repaint();

Although,when an exception occurs and a JOptionPane is displayed,the progressbar is displayed until the dialog is closed and then returns to the normal UI.Can you help me out??Any and all answers are appreciated :)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
sumanth
  • 135
  • 1
  • 10
  • 4
    Swing is single threaded and not thread safe. I presume that you are blocking the event dispatching thread, see [Concurrency in Swing](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html) for more details and [Worker Threads and SwingWorker](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for a possible solution – MadProgrammer Jun 03 '19 at 04:58
  • 2
    For [example](https://stackoverflow.com/questions/24835638/issues-with-swingworker-and-jprogressbar/24835935#24835935), [example](https://stackoverflow.com/questions/22031086/jprogressbar-not-working-properly/22031159#22031159), [example](https://stackoverflow.com/questions/53916360/how-to-run-method-with-jprogressbar/53917359#53917359) – MadProgrammer Jun 03 '19 at 05:00
  • @MadProgrammer thanks i just checked it out.so it's like asynctask in android.But it has doInbackgroud() and done()....done() executes code after thread executes..any function to execute code before thread executes?? – sumanth Jun 03 '19 at 05:25
  • hey thanks..i just used SwingUtilities.invokeandwait before code in doInBackground()..it worked – sumanth Jun 03 '19 at 05:56
  • That would depended, but I would execute what ever you need to do BEFORE calling `execute` and then do what you need to do in the background inside the `SwingWorker` – MadProgrammer Jun 03 '19 at 08:14

0 Answers0