Solved

JPanel Swing setting focus in what event ?

Posted on 2006-07-05
14
501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

691 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