Saikiran Gosikonda
Saikiran Gosikonda

Reputation: 1035

How to change the modality of a dialog while it is visible

I have a JDialog for which I set the modality as model less. But, now I need to modify its modality while it is visible. But I know that Swing does not allow to change the modality of a dialog while it is visible. So, is there any other tricks or code that can do this?

Upvotes: 2

Views: 1060

Answers (2)

Timmos
Timmos

Reputation: 3324

Just doing what the Javadoc says... hide and show the dialog.

Note: changing modality of the visible dialog may have no effect until it is hidden and then shown again.

So here's a working example:

public class ModalDialogTest {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame ();
                final JDialog dialog = new JDialog(frame);
                dialog.setModal(true);

                Runnable modalSwitcher = new Runnable() {
                    @Override
                    public void run() {
                        try {
                            System.out.println("Worker thread: sleeping for 5 seconds");
                            Thread.sleep(5000);
                            System.out.println("Worker thread: sleeping finished!");
                        } catch (InterruptedException ex) {
                            // this thread is not interrupted
                        }
                        SwingUtilities.invokeLater(new Runnable() {
                            @Override
                            public void run() {
                                System.out.println("ENTERING MODALSWITCHER!");

                                dialog.setModalityType(ModalityType.MODELESS);
                                dialog.setVisible(false);
                                dialog.setVisible(true);

                                System.out.println("NO LONGER MODAL!");
                            }
                        });
                    }
                };
                new Thread(modalSwitcher).start();

                System.out.println("DIALOG WILL NOW SHOW UP:");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
                dialog.setVisible(true);
                System.out.println("BYE!");
            }
        });
    }
}

After 5 seconds, you'll be able to click on the parent JFrame.

This is what the program prints:

DIALOG WILL NOW SHOW UP:
Worker thread: sleeping for 5 seconds
Worker thread: sleeping finished!
ENTERING MODALSWITCHER!
NO LONGER MODAL!
BYE!

Whether you really want to continue with the practice of switching modality while showing a dialog is up to you, but I do not recommend it in the perspective of user experience - it feels rather odd.

Upvotes: 3

Giriraj
Giriraj

Reputation: 21

update the dialog box and call repaint() on it. If you have changed the hierarchy (added/removed components) then don't forget to revalidate().

Upvotes: -1

Related Questions