Solved

JPanel Swing setting focus in what event ?

Posted on 2006-07-05
14
488 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now