Note that there are some explanatory texts on larger screens.

plurals
  1. PODifferent behavior of JDialog when is created by program and by user action
    text
    copied!<p>I have 3 clasess : Loader, MyDialog and TEST(with main method). (for code see below)</p> <p>Everything I want to achieve is create simple dialog with JLabel and JProgressBar, which will notify user about how much time remains to show MyDialog. MyDialog is Jdialog with time consuming operation in constructor (loading data from database etc.).</p> <p>In code below is model situation. When "MyDialog" is created by main (constant BY_USER is false), everything working exactly i want to. But when i make dialog with button , and instance of MyDialog is created after button press (constant BY_USER is true), Loader is blank white form. It looks like is not completed.</p> <p>Loader is extending Thread, so i suppose that problem will be in threading (event dispatch thread)? I dont know, what is wrong and how fix it. Please help. </p> <p>Thanks and sorry for my English.</p> <p>CLASS TEST :</p> <pre><code>package test; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.WindowConstants; public class TEST { public static final boolean BY_USER = false; public static void main(String[] args) { if (BY_USER) { JFrame mainDialog = new JFrame("Main"); JButton show = new JButton("Show MyDialog"); show.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { MyDialog dialog = new MyDialog(); } }); mainDialog.add(show); mainDialog.setLocationRelativeTo(null); mainDialog.setMinimumSize(new Dimension(160, 80)); mainDialog.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); mainDialog.setVisible(true); } else { MyDialog dialog = new MyDialog(); } } } </code></pre> <p>CLASS MyDialog : package test;</p> <pre><code>import javax.swing.JFrame; import javax.swing.WindowConstants; public class MyDialog extends JFrame{ public MyDialog() { super(); // making loader with title, first message and count of steps of operation Loader loader = new Loader("Loader", "First showed message", 100); loader.ShowLoader(); // time-consuming operation (loading data from database etc.). // for clarity replaced with for statement int j=0; for(int i=0; i&lt;Integer.MAX_VALUE; i++) { j++; if(j==Integer.MAX_VALUE/100){ // updating loader message and progress bar value loader.NewAction(Integer.MAX_VALUE - i+""); j=0; } } // closing loader loader.DestroyLoader(); this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); this.setSize(300, 300); this.setLocationRelativeTo(null); this.setVisible(true); } } </code></pre> <p>CLASS Loader:</p> <pre><code>package test; import java.awt.BorderLayout; import java.awt.Dialog; import java.awt.Dimension; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JProgressBar; import javax.swing.SwingConstants; public class Loader extends Thread{ private JDialog dialog; private JLabel message = new JLabel("", SwingConstants.CENTER); private JProgressBar progressBar = new JProgressBar(0, 100); private String newMessage; private double percentForStep; private int remainingSteps; public Loader(String taskName, String firstMessage, int steps) { this.remainingSteps = steps-1; dialog = new JDialog((Dialog) null, taskName); dialog.setLayout(new BorderLayout(15, 15)); dialog.add(message, BorderLayout.CENTER); dialog.add(progressBar, BorderLayout.SOUTH); message.setText(firstMessage); percentForStep = 100 / steps; } public void ShowLoader() { dialog.setMinimumSize(new Dimension(400,120)); dialog.setLocationRelativeTo(null); dialog.setVisible(true); this.start(); } public void DestroyLoader(){ dialog.dispose(); this.interrupt(); } public void NewAction(String newMessage){ this.newMessage = newMessage; this.remainingSteps--; Lock.changed = true; } public int RemainingStepsCount() { return remainingSteps; } @Override @SuppressWarnings({"CallToThreadYield", "SleepWhileInLoop"}) public void run() { do{ synchronized (Lock.class) { if (Lock.changed) { Lock.changed = false; this.message.setText(newMessage); this.progressBar.setValue((int)(100-(remainingSteps*percentForStep))); dialog.repaint(); } dialog.repaint(); } }while(true); } } class Lock{ static boolean changed = false; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload