This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Modal dialog box and request focus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Modal dialog box and request focus " Watch "Modal dialog box and request focus " New topic
Author

Modal dialog box and request focus

Sumana Reddy
Greenhorn

Joined: Oct 28, 2000
Posts: 14
I have a modal dialog box.When ever I make the dialog box visible I need to request the focus for a particular button.
below is the description of how I tried to achieve the functionality
I can request the focus by calling requestFocus method.We should request the focus only when the component is visible i.e only after i make the dialog box visible.But when a modal dialog box is visible it blocks the application and the requestFoucs method is not executed as long as the dialog is visible.
So in order to solve this problem I decided not to make my dialog modal during creation time.When I make my dialog visible, First i will request the focus and then make the dialog modal.But once the dialog is visible the modal concept is not working.thoght the setModal() is executed and the dialog.isModal() returns true But somehow the dialog is not modal on the application.
So I tried to implement this modal functionality as you suggested i.e enabled and disabling the parent.That works pretty fine.But when I close the dialog box The parent frame is hidden and someother window that's on task bar will be in the front.
So when I close the dialog I call the parent.toFront() to make it appear infront of all windows.when the dialog is closed the parent frame will come to front after some flickering.That flickering is real bad.
Is there any better solution for the above problem.
Thanks Sumana
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Since you're using a button you could use the setDefaultButton() method of JRootPane...

Here's a short example program showing how to use setDefaultButton() :



If you need to request focus for another kind of component, you may need to do so in a ComponentListener's componentShown() method attached to the dialog.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Modal dialog box and request focus