Solved

Set focus on JComboBox

Posted on 2004-04-02
13
2,175 Views
Last Modified: 2012-06-21
How can i set focus on JComboBox??? i checked the JComboBox API but didn't get the setFocus property.

thanks
0
Comment
Question by:Kennywen
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 25 total points
ID: 10739254
combo.requestFocus();
0
 

Author Comment

by:Kennywen
ID: 10739268
JComboBox dBox = new JComboBox(dLists);
dBox.addActionListener(this);
dBox.setActionCommand("Combo1");
dBox.requestFocus();

but the combobox doesn't get focus.

thanks
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10739293
Try an updateUI () call for your container (which contains the JComboBox) after requestFocus ().
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10739300
You better wait to perform

         dBox.requestFocus();

until it is really needed and after all (window/frame/dialog) initialization is done.

Maybe you can post more complete code
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10739341
You really need to do this in a window listener if you want this to happen as soon as your app is shown. See

http://javaalmanac.com/egs/java.awt/focus_InitialFocus.html
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 10739369
Can it not be done in a statement just after show () or setVisible ( true ) ?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

Expert Comment

by:mayankeagle
ID: 10739376
I mean - the updateUI () ?
0
 

Expert Comment

by:RobCSP
ID: 10739380
Try it:


combo.requestFocusInWindow();


I hope that it  helps,

ROB
0
 

Author Comment

by:Kennywen
ID: 10739397
i solve the problem by using coding below:

myFrame.setResizable(false);
dBox.requestFocus();
myFrame.setVisible(true);

any disadvantage on above code?

thanks
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10739413
If what i first suggested is working, then stick with that
0
 

Author Comment

by:Kennywen
ID: 10739422
sure i will but just want to know any disadvantage on it?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10739434
Not having a deep enough knowledge of focus - i'd probably resort to trial and error myself ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10739456
8-)
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

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

21 Experts available now in Live!

Get 1:1 Help Now