Search code examples
javafocusjdialog

.setModal() method on a JDialog


I want to make i so that the parent window cannot be accessed whilst the child window is active. I'm using the .setModal() method of the JDialog class, but it doesn't seem to be working.

   if (clickEvent.getSource().equals(addCheckout1)){

        SupermarketCheckoutGUI checkout1 = new SupermarketCheckoutGUI();
        checkout1.setVisible(true);
        checkout1.setSize(670, 400);

        checkout1.setModal(false);
    }

Just to check, I've tried both true and false, but there is no change.


Solution

  • From the docs:

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

    Try calling setModal(true) before setVisible.

    But setModal is deprecated, you should call setModalityType instead (the type you need is likely APPLICATION_MODAL), check this tutorial.