Solved

JPanel Swing setting focus in what event ?

Posted on 2006-07-05
14
497 Views
Last Modified: 2010-03-31
I am using Swing. In a Jpanel descendant constructpr at the end I set :

        myeditbox.setFocusable(true);
        myeditbox.requestFocus();

However when the form is shown the editbox does not have focus, none of the controls on the form do.

Do I have to do this in another event?

Thanks, Tom.
0
Comment
Question by:tomcorcoran
  • 7
  • 5
  • 2
14 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 17043173
Hi tomcorcoran

You have to dispatch the focus event for the desired component, have a look here for a possible solution: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21872206.html

Cheers
0
 
LVL 8

Expert Comment

by:colr__
ID: 17043279
Is this an applet or an application?
0
 

Author Comment

by:tomcorcoran
ID: 17043445
Ok. Dispatching the focus event is goi9ng over my head. It seems I have to write a lot of code to be able to focus a component when a Jpanel descendant is shown?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tomcorcoran
ID: 17043456
I'm working with a Japplet descendant.
0
 
LVL 8

Expert Comment

by:colr__
ID: 17043514
When you click on the applet is focus set to the correct part, even if you dont necessarily click on that part (example, click on some of the grey dead area of the applet)? If so, then the focus is being set right on the applet, but the applet itself isnt getting focus on the html page, which means you'll probably need to do this with javascript.
0
 

Author Comment

by:tomcorcoran
ID: 17043614
Focus is not set when I click on the applet.
0
 
LVL 8

Expert Comment

by:colr__
ID: 17043634
Ok, try giving your applet an id: <applet id="theApplet" codebase= ...></applet>
Then put this in the body tag: <body onLoad="document.getElemenetById("theApplet").focus()">

Let me know how this goes.
0
 

Author Comment

by:tomcorcoran
ID: 17044180
I'm not using code like that. I am hoping fo a Java solution. Is it possible?
0
 
LVL 8

Accepted Solution

by:
colr__ earned 50 total points
ID: 17044447
Your right, there is a method (I just checked an applet Ive got). try this:

myeditbox.requestFocusInWindow();

Ive got this working as a replcement for the methods you've tried above.
0
 

Author Comment

by:tomcorcoran
ID: 17050746
I did see that working the first time I tried it, so you deserve the points, thanks.

I did get it working one other time but however, I can no longer get it to work now. A strange one.

Tom.
0
 

Author Comment

by:tomcorcoran
ID: 17050792
I am using 1.5, maybe this bug affects things
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5089436
though I am not alt-tabing.
0
 
LVL 8

Expert Comment

by:colr__
ID: 17050895
That bug has been fixed since beta 2 of Tiger. Are you using the beta version?
0
 

Author Comment

by:tomcorcoran
ID: 17071210
I am using v1_5_0_05. Don't know why it worked just one time :-( From searching the web it does seem to be a problem people experience.
0
 
LVL 35

Expert Comment

by:girionis
ID: 17071217
requestFocusInWindows() does not work, that is explained in the link I posted, that's why the solution suggested was complicated.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 58
Is Applet the way to go for my drag and drop system? 8 35
junit as external jar or library 7 38
Tomcat 9 + java 8 error while trying to deploy a war file 2 711
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question