Model JDialog does not show content

(Java SE 6) I am trying to create a popup dialog that displays the message "please wait" while my program does some time-consuming work. For this, I decided to use the model JDialog so that the program continues to work and works while JDialog is visible (if you use the modal, the program will stop until the user closes the dialog).

The problem is that when you use a modeless dialog in this way, a JDialog appears with a heading, but the contents do not work (I assume that for some reason they are not painted). I tried renaming, etc., but nothing works.

Now, according to a group of people on the firewalls, this is a real mistake with Swing, and the only answers I found were not to do it this way and find other ways to notify the user to wait. I am curious if anyone has this question before and figured out how this works.

Thanks!

+3
source share
4 answers

As an alternative, consider using SwingWorker and show the intermediate progress as suggested in this example .

+4
source

This is not a mistake, you need to start the hard work and do light weight work in the saddle thread next to the main thread. This is necessary due to logical violations between the GUI dialog dialog box and its ActionListenerEvents attitude towards the heavy work of the scales in the background. If you do not share your main thread, you will be able to track the Swing draw due to some event notifications. I had the same problem, I tried to track the progress of the FTP upload that I started with JFrame to show it in 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 bind the JDialog FrameBorder and the balck content panel, but ...

Next attempt:

 //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 this helps;)

+5
source

I use this and it works - the source code (without the fix I found for redrawing when I run it) is here: http://inversionconsulting.blogspot.com/2008/03/java-jdialog-and-jprogressbar-example .html

but I strengthened it (with minor changes):

 JProgressBar pb = new JProgressBar(0,100); pb.setPreferredSize(new Dimension(275,30)); pb.setString("Running"); pb.setStringPainted(true); pb.setValue(0); JLabel label = new JLabel("Progress: "); JPanel center_panel = new JPanel(); center_panel.add(label); center_panel.add(pb); JDialog dialog = new JDialog((JFrame)null, "Working ..."); dialog.getContentPane().add(center_panel, BorderLayout.CENTER); dialog.pack(); dialog.setVisible(true); dialog.setLocationRelativeTo(null); // center on screen 

In my code later, when it runs through my loop (from 1 to 10 using the variable "tot"), I redraw the dialog by updating the progress bar when the program goes through the loop this way (this is not mentioned in the link above) (I I do not use multiple threads, all this in the main thread):

 //set progress bar pb.setValue(tot*10); //repaint it dialog.getContentPane().paintAll(pb.getGraphics()); 

To find this solution, it took a lot of time and trial and error. Hope this works for you as well as for me.

+2
source

Have you tried validate () (this is necessary after adding components to the container)? Also consider sending the source code so that we can verify it. Good luck.

0
source

Source: https://habr.com/ru/post/1384780/


All Articles