?
Solved

JPanel Swing setting focus in what event ?

Posted on 2006-07-05
14
Medium Priority
?
503 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 200 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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:
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

801 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