Solved

JPanel Swing setting focus in what event ?

Posted on 2006-07-05
14
493 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…

832 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