wood burning stoves 2.0
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Modal dialog box and request focus " Watch "Modal dialog box and request focus " New topic

Modal dialog box and request focus

Sumana Reddy

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

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.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
I agree. Here's the link: http://aspose.com/file-tools
subject: Modal dialog box and request focus
It's not a secret anymore!