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?
Asked
Active
Viewed 969 times
2

Saikiran Gosikonda
- 928
- 1
- 11
- 29
-
There's probably no such trick besides doing what the JavaDoc says, i.e. hide and show the dialog again. The main question would be: why do you want to do that? Don't you know the modality before displaying the dialog? How should the user handle a dialog that is hidden by some window (probably modal as well) when it suddenly becomes modal? – Thomas Feb 25 '16 at 08:49
-
@Thomas, Actually I got into a situation where I have a dialog with one text field in it. I set the default focus for this text field. But, when my dialog is modal, then it is working fine. But, when my dialog is modelless the focus is not getting to the text field automatically after opening the dialog – Saikiran Gosikonda Feb 25 '16 at 08:54
-
So the problem is the dialog not getting or losing the focus? I'd work on that then instead of making the dialog modal. – Thomas Feb 25 '16 at 09:19
-
@Thomas, Dialog is not getting the focus – Saikiran Gosikonda Feb 25 '16 at 09:22
-
Ok, then as I said try to solve that instead of struggling with a workaround (btw, are you sure the dialog doesn't get the focus and lose it immediately due to some other component requesting it?). – Thomas Feb 25 '16 at 09:26
-
yes, no other component is requesting the focus. And the text field is getting focus if the dialog is modal – Saikiran Gosikonda Feb 25 '16 at 09:30
-
Hmm, what I meant is: it might be that the textfield always gets the focus and something else you might not even know about tries to steal it - which would be prevented if the dialog is modal. Thus it _might_ look like the textfield only "gets" the focus when the dialog is model but actually it's only _keeping_ the focus then. – Thomas Feb 25 '16 at 09:34
2 Answers
3
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.

Timmos
- 3,215
- 2
- 32
- 40
-1
update the dialog box and call repaint()
on it. If you have changed the hierarchy (added/removed components) then don't forget to revalidate()
.

Giriraj
- 21
- 4
-
-
1I fail to see why this answer is upvoted so much. A `repaint()` has not the effect of the dialog's modality being re-evaluated. – Timmos Feb 25 '16 at 09:33
-
You can't even call `revalidate()` on a JDialog. Are people blindly upvoting or what? – Timmos Feb 25 '16 at 10:35