Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes showInputDialog( ) focus Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "showInputDialog( ) focus" Watch "showInputDialog( ) focus" New topic
Author

showInputDialog( ) focus

Rob Byrd
Greenhorn

Joined: Jul 13, 2007
Posts: 12
I have an application in which the user drags an object onto a canvas. When the drop occurs the drop target (canvas) executes the statement:
data = JOptionPane.showInputDialog(...). The cursor is blinking in the dialog's text field, but the user has to click on the field (or the dialog window) before input is accepted. Is there a way to automatically set focus for the dialog so the user doesn't have to click it first? I don't see how to use requestFocus() on a modal dialog.
Thanks for our help.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

It looks like you're mixing AWT and Swing components* here. That may or may not be the cause of your problem, but is better avoided.

I suggest you change your code to use JComponent or JPanel in place of Canvas. You would want to override paintComponent(...) instead of paint(...).

Additionally, if your top-level window is AWT Frame, you would need to change that to JFrame.

*edit Added the word components
[ May 23, 2008: Message edited by: Darryl Burke ]

luck, db
There are no new questions, but there may be new answers.
Rob Byrd
Greenhorn

Joined: Jul 13, 2007
Posts: 12
Sorry about that. I was using the word canvas in a graphics sense. My canvas object is subclassed from JPanel. I'm using Swing for everything.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

OK. In that case, it sounds like a concurrency issue. You could try wrapping the call to JOptionPane.showInputDialog in a SwingUtilities.invokeLater.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: showInputDialog( ) focus
 
Similar Threads
How to have a drop down sort of an item?
Setting initial focus in a Model JDialog
Render a panelconfirmation conditionally
Two focused window at the same time
requestFocus() is not working across applications