My Java development and Android findings, thoughts and rants. I expect to mainly post about JDesktop and Android related issues but will no doubt go off topic many many times.

I'm writing this as someone who actually uses Java at the coal face and not as someone who sits in an ivory tower quoting happy day scenarios. So don't expect perfect code, full use of patterns. I'll be posting about stuff that is actually useful for me and with any luck others!

Core Java Books

Thursday, 10 March 2011

JInternalFrame's Parent & Child Modality

This post is about managing JInternalFrame's within a JDesktopPane, specifically the ability to popup child JInternalFrame's from a parent JInternalFrame's. Now why would you want to do this you may ask? Well on several applications I have worked on users open forms to view/enter data, these forms then offer extra buttons to view related data in other forms. In many cases I require that the new form blocks the parent form, in short becomes modal to the parent form (not other forms).

This post offers code and a demo of a simple extension to JInternalFrame which controls the child and parent relationship. It also offers non desktop modal dialogs, but dialogs that are only modal to the parent JInternalFrame.

The functionality the provided specifically is:

Frames can open child frames. The parent frame becomes busied out (lightweight modal).

Clicking on a busied out parent frame brings the top related child frame to the front.

Multiple parent and child hierarchy's can be on the desktop at one time.

A frame can open a dialog, the dialog can be either desktop modal or modal just to the frame hierarchy it was opened from.

A frame modal dialog will be brought to the front if any of its parent frames are clicked on.

Please try out the demo, the list below gives some idea of the functionality.

Play around with the demo, the menu lets you open many frames that are not related. Each frame then has buttons to let you open different children. Notice how you can click on different children and children of children and also busied out parents, you will see that the top most child frame of the associated from clicked is always brought to the front, even if iconized.

I think many people will find this functionality useful in JDesktopPane related applications

Basically I have overridden JInternalFrame and added functionality to listen out for child frames opening and closing. This caused glassplanes to be installed or removed as appropriate. Check out the logic, its easy(ish) to follow. Let me know if you can think of other uses or extensions to this.

In future I will create a post to demonstrate full modality for internal frames ;)

UPDATE: AUG 2016 - Google have retired the Google Drive Archive :( If you wish to obtain the code please email me and I can send it via email or you can obtain the whole archive here.

16 comments:

Hi Steve. Very helpful so far, but I've noticed that if the parent has the closable attribute set to true, when the child is closed, it did not set back the closable to the parent frame. I've added to my code cause it's important for me that behavior. Thanks.. Ulises Estecche (estecche@gmail.com)

I also was looking for that. There are other solutions being way more complicated in implementation. Excellent work.I agree with Ulises. It seems you forgot a little line for the correct closeable handling in a parent after the child was closed:

Yes you are correct that change is needed. Interestingly it wasn't required when I tested the code with the latest version of Java when it was written so obviously something in the JDK was changed which now means it is required.

Thank you so much.. I was digging the forums for weeks to get this code.. Itz really helpful.. Thanks a lot. I have been using JDialogs before.. But i missed one feature. ModalityInternalFrame cannot return a value.. Any way thanks

Hi Thanks for this class. I have a problem with "ModalityInternalFrame".When I create/Show it from "actionPerformed" event of a "JMenuItem" it uses 100% of CPU when it is visible! (But showing from actionPerformed" event of a "JButton" has no problem!!)What is the problem?

No problem Daniel I'm glad some of the stuff on my old blog is still useful. I haven't been working on Swing for a while now as I'm doing web based work but it would be nice to take a look at Swing and JavaFX again at some point.