How to add JProgressBar into JDialog

1.3k views Asked by At

I'm trying to add JProgressBar to JDialog but JProgressBar never show up. Below is my code. Any help?

        JDialog downloadingDialog = new JDialog(jbpci ,"Start donwloading...");
        JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL);
        progressBar.setIndeterminate(true);
        downloadingDialog.setLayout(new FlowLayout(FlowLayout.LEFT));
        downloadingDialog.add(progressBar);
        downloadingDialog.setLocation(jbpci.getLocationOnScreen().x + jbpci.getWidth() / 2, jbpci.getLocationOnScreen().y + jbpci.getHeight() / 2);
        downloadingDialog.setSize(300, 100);
        downloadingDialog.setVisible(true);
1

There are 1 answers

0
Roberto Attias On

I wrapped your code in a class and removed references to jbpci, and it works fine:

import javax.swing.*;
import java.awt.*;
public class t {

    public static void main(String[] args) {
        JDialog downloadingDialog = new JDialog((JFrame)null ,"Start donwloading...");
        JProgressBar progressBar = new JProgressBar(JProgressBar.HORIZONTAL);
        progressBar.setIndeterminate(true);
        downloadingDialog.setLayout(new FlowLayout(FlowLayout.LEFT));
        downloadingDialog.add(progressBar);
        downloadingDialog.setSize(300, 100);
        downloadingDialog.setVisible(true);
    }
}