1

Why this code do not stop when the while loop is empty. If I add an instruction the code work fine. Normally after the the user clicked an button the test variable will be changed so the loop will ends. Is there another way to test that the JDialog was disposed.

public class FenetreAjoutClass extends JDialog {
    private JPanel pan = new JPanel();
    private JPanel buttPan = new JPanel();
    private JTextField schoolLevl = new JTextField();
    private JButton valide = new JButton("OK");
    private static String infos = null;
    private static boolean test = false;

    private JButton cancel = new JButton("CANCEL");

    FenetreAjoutClass(JFrame parent, Boolean modal) {
        valide.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                infos = schoolLevl.getText();
                test = true;
                dispose();

            }
        });

        cancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                test = true;
                dispose();

            }
        });
        this.setLocationRelativeTo(null);
        this.setResizable(true);
        this.setLayout(new BorderLayout());
        pan.setLayout(new GridLayout(1, 1));
        pan.add(schoolLevl);
        this.add(pan, BorderLayout.NORTH);
        buttPan.add(valide);
        buttPan.add(cancel);
        this.add(buttPan, BorderLayout.SOUTH);
        pack();
        setVisible(true);
    }

    public static void main(String[] args) {
        System.out.println(get());
    }

    public static String get() {
        new FenetreAjoutClass(null, false);
        while (!test) {
            //System.out.println(test);
        }
        return infos;
    }
}
Suzon
  • 749
  • 1
  • 8
  • 21
Hunsu
  • 3,281
  • 7
  • 29
  • 64
  • 1
    Perhaps the compiler optimizes it _away_ if the loop doesn't have a statement. – devnull Mar 08 '14 at 13:12
  • 1
    Yes, there are other ways, and generally speaking, the sort of infinite loop you have (called a *spin*) is the worst way to implement waiting, because it makes the computer do lots of useless work instead of doing something else or going into power-save mode. – chrylis -cautiouslyoptimistic- Mar 08 '14 at 13:14
  • I have done that for testing. Another question, can we tell the JVM to not do optimization. – Hunsu Mar 08 '14 at 13:17
  • 3
    (1) If you really want to check like this, you have to make `test` `volatile` so that the compiler will know it needs to keep checking the variable every time. (2) The correct way to handle this is to move whatever you were going to do after the `while` loop into the listeners, where you currently have the `test = true` statements. Pass in a `Runnable` to be invoked on a `SwingWorker` if you can't specify ahead of time what tasks they will be. – chrylis -cautiouslyoptimistic- Mar 08 '14 at 13:26

1 Answers1

2

The dispose will free up your memory. All data for the dialog are gone. If you want to show the window later again you have to work with visibility. This can be checked with isVisible().

You can replace the dispose() in your code with this.setVisible(false)

public static String get() {
        FenetreAjoutClass dialog = new FenetreAjoutClass(null, false);
        while (dialog.isVisible()) {
            System.out.println("is Visible");
        }
        System.out.println("is not Visible");
        return infos;
}

Mind that the console will still print "is Visible" over a short time after the dialog is closed. But this is because the console can does not print as quick as the while loop restarts.

Jan
  • 86
  • 3