Skip to content
Advertisement

how to configure joptionpane with no button to dispose()?

i would like to display a message while initialisation of data using a dialog.

But it does’t want to close and I can’t see what is wrong with this code :

final JOptionPane optionPane = new JOptionPane("Information ", JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
JDialog dialog1 = optionPane.createDialog(null,"Wait for init");

dialog1.setAlwaysOnTop(true);
dialog1.setVisible(true);
dialog1.setModal(false);

dialog1.dispose();

Thank you

Advertisement

Answer

This…

dialog1.setVisible(true);
dialog1.setModal(false);

is wrong. Once the dialog is made visible, it will wait till it’s closed before continuing the flow of execution, it’s one of the neat side effects of modal dialogs

I also suspect that you’re violating the single threaded nature of Swing.

See:

for more details

Runnable example…

This example shows a dialog, waits for 5 seconds (in the background) and then closes the window. A neat side effect of this is, the window can’t be closed by the user 😈

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;

public class Main {

    public static void main(String[] args) {
        new Main();
    }

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JDialog dialog = new JDialog();
                dialog.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosed(WindowEvent e) {
                        System.out.println("Closed");
                    }
                });
                dialog.add(new WaitPane());
                dialog.setAlwaysOnTop(true);
                dialog.setModal(true);
                dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
                dialog.pack();
                dialog.setLocationRelativeTo(null);

                new SwingWorker<Void, Void>() {
                    @Override
                    protected Void doInBackground() throws Exception {
                        Thread.sleep(5000);
                        return null;
                    }

                    @Override
                    protected void done() {
                        System.out.println("Done");
                        dialog.dispose();
                        dialog.setVisible(false);
                    }                    
                }.execute();

                dialog.setVisible(true);
            }
        });
    }

    public class WaitPane extends JPanel {

        public WaitPane() {
            setBorder(new EmptyBorder(64, 64, 64, 64));
            setLayout(new GridBagLayout());
            add(new JLabel("Wait for init"));
        }

    }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement