问题
I have an MDI application which uses JInternalFrames. Each internal frame does a different operation, however, some of the other frames display messages using JOptionPane, which of course, pauses the entire application.
Is there anyway to (whenever a JOptionPane dialog or any other dialog box for that matter) be modal to its OWN internal frame (i.e. frame X displays a message, pauses its OWN frame, but allows frame Y and the rest of the application to continue)?
And is it possible to do this without having to change the code (or at least only a little of it)?
回答1:
I see what you're asking, it might be possible but it definitely won't be easy. You want the modality to be "local" to the JInternalFrame that popped up the modal dialog.
First you need to understand that there are really 2 parts to a modal pop-up that can be approximated by something of your own creation. 1) The code that sets the modal pop-up blocks until the pop-up closes, and 2) The "background" does not respond to GUI events while the modal pop-up is visible.
True modality achieves this by blocking the current EDT and creating a new event pump for the modal component. (See java.awt.Container.#startLWModal()) This isn't going to work for you, because all of your JInternalFrames share 1 EDT, which is pretty fundamental to how Swing works (single UI thread)
However, your JInternalFrames are JRootPanes, which means that they have glasspanes. You can use this to create your own modality, of sorts. The idea is to have your modal pop-up for each JInternalFrame appear centered on a transparent glasspane that gets installed on the JInternalFrame. Add a mouse listener that consumes mouse events to the transparent glasspane, this will get you modality feature #2 (background seems non-responsive). Use OO instead of blocking to get feature #1 (Have your modal popup take an IModalPopupListener (I made this up -you'll have to create it) object to call back when the modal pop-up dissapears).
I hope this make sense! Good luck!
回答2:
This looks promising:
Creating Modal Internal Frames
Edit
Responding to your comment (from linked page):
There are times however when you might want a dialog in an internal frame to be modal. This Tech Tip will show you how to create a modal dialog in an internal frame.
Isn't it exactly what your looking for?
来源:https://stackoverflow.com/questions/1420276/display-a-modal-dialog-box-in-a-jinternalframe-without-pausing-application