Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modeless JDialog not showing contents

(Java SE 6) I'm trying to create a pop-up dialog that displays a "please wait" message while my program does some time-intensive work. To do this, I've chosen to use a modeless JDialog, so that the program continues to run and does work while the JDialog is visible (if you use a modal one, the program will halt until the dialog is closed by the user).

The problem is that when you use a modeless dialog in this fashion the JDialog with a title appears, but the contents don't (I'm guessing they don't get painted for some reason). I've tried calling repaint etc but nothing seems to work.

Now, according to a bunch of people on the interwebs this is a genuine bug with Swing, and the only answers I've found are to not do things this way and find some other means of notifying the user to wait. I'm curious if anyone here has had this issue before and has figured out a work-around.

Thanks!

like image 342
Riemannliness Avatar asked Dec 21 '22 16:12

Riemannliness


1 Answers

It´s not a bug, you have to run your heavy weight job and your light weight swing job in sepred thread´s next to the main thread. It´s necessary because of the logical inflictions between the Dialog GUI Thread and it´s ActionListenerEvents relation to the heavy weigth job in the backround. If you don´t seperate your Main thread will deterinate the Swing draw due to some notify Events. I had the same Problem, I tryed to monitor the progress of an FTP Upload Progress which I started out of a JFrame to show it in an JDialog.

First I tried:

//Activated by Upload Button
public void actionPerformed(ActionEvent e) {

    if("Upload".equals(e.getActionCommand())){

    // some Declarations

    new Thread(){

        public void run() {
            /*Run JDialog with the Upload - ProgressBar*/
            FileUploadProgressBar fileUploadProgressBar = new FileUploadProgressBar(localFile, remoteFile, ftpPersistence);
        }
    }.start();

/*Run the heavy weigth Job - the Upload*/
ftpPersistence.uploadFile(localFile, remoteFile);

// ...
    }

//...

}

But this way i gaint a JDialog FrameBorder ans a balck Content Pane but ...

Next try:

//Activated by Upload Button
public void actionPerformed(ActionEvent e) {

    if("Upload".equals(e.getActionCommand())){

    // some Declarations

    new Thread(){

        public void run() {
        /*Run JDialog with the Upload - ProgressBar*/
        FileUploadProgressBar fileUploadProgressBar = new FileUploadProgressBar(localFile, remoteFile, ftpPersistence);
    }
}.start();


new Thread(){

    public void run() 
            /*Run the heavy weigth Job - the Upload*/
            ftpPersistence.uploadFile(localFile, remoteFile);
        }
    }.start();
    // ... 
    }
//...
}

and finally it worked, hope it will help ;)

like image 190
elfu Avatar answered Dec 28 '22 23:12

elfu