• Post Reply Bookmark Topic Watch Topic
  • New Topic

setModal issue for JFrame and 2 Jdialogs created

 
hima reddy
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing issue when I setmodal=true for first Jdialog and setmodal=false for second Jdialog

Below is the functionality I am trying to implement
1)On click of Test the dialog! button, JDialog with name Custom Dialog Main will open.
2)Click "yes" in JDialog with name Custom Dialog Main, Another JDialog with name CustomDialog Search will open
3)Click "yes" in JDialog with name CustomDialog Search, JDialog with name Custom Dialog Main should come front.
4)And I should be able to select any JDialog[For example if I select JDialog with name CustomDialog Search, the other Dialog should go back and vice versa ]

Problem I am facing is When I Click "yes" in JDialog with name Custom Dialog Main, the JDialog with name CustomDialog Search is displayed behind the Main Jdialog.
This is happening because I am setting setmodal=false for CustomDialogSearch.
If I setmodal=true CustomDialogSearch it is displayed correctly but after I click yes CustomDialog Main doesn't come front.

I even tried to set CustomDialogSearch's parent to be CustomDialog still the behaviour is not correct
Below is the example code I am testing




Jdialog.PNG
[Thumbnail for Jdialog.PNG]
Jdialogs
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using a wrong parent for the CustomDialogSearch dialog. You need to use the CustomDialog instance as the parent (and change the constructor declaration to allow for this). Parents matter in Swing, especially when modality is concerned.

Edit: I've given you a cow for providing a SSCCE.
 
hima reddy
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks You.
Even after I have specified the parent as CustomDialog for CustomDialogSearch, CustomDialog is never moved toFront() eventhough the focus has changed to CustomDialog as shown in below screenshot.

In stackoverflow forum one person explained saying it depends on windows OS as well. http://stackoverflow.com/questions/22037658/setmodal-issue-with-2-jdialogs-within-a-jframe/22041971?noredirect=1#comment33487708_22041971

Just wanted to confirm from code ranch members/team if they have any other insights



SetParentaAsCustomdialog.PNG
[Thumbnail for SetParentaAsCustomdialog.PNG]
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't noticed you've mentioned in your original post you'd already tried this. Sorry, my bad.

I'm not sure it is possible to get the exact behavior you describe. Child windows are always displayed over their parents, and you need this child-parent relationship to enable your modeless dialog over the modal one.

If you could create a third modal dialog, and set it as a parent for both CustomDialog and CustomDialogSearch (both non-modal), then you could focus either one and the focused one would display on top. Perhaps the third dialog could be made so small (say, just a pixel, strategically placed) that the user wouldn't notice it, but this sounds like a very nasty hack and you'd need to test this thoroughly on all platforms you want to support.

If you don't want the third dialog, you might still get his behavior if you make them both modeless and manually disable all other windows in your application (activating glass panes and so forth), thus simulating the modality of both of your dialogs. Having a look at what Swing does internally when activating a modal form might also help. But it looks like a lot of work either way.

Yet another option would be to integrate both CustomDialog and CustomDialogSearch into one (using CardLayout perhaps), which could then be made modal and you would save yourself a lot of hassle.
 
hima reddy
Greenhorn
Posts: 9
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried your last Option to put both JDialogs in one CardLayout except for setting the modal.
The functionality between the JDialogs work perfect but because modality is not set both the Dialogs are moving behind the JFrame.
Can you please suggest how to setmodality after integrating both JDialogs to CardLayout




 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!