Set focus on JComboBox

How can i set focus on JComboBox??? i checked the JComboBox API but didn't get the setFocus property.

thanks
KennywenAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
combo.requestFocus();
0
 
KennywenAuthor Commented:
JComboBox dBox = new JComboBox(dLists);
dBox.addActionListener(this);
dBox.setActionCommand("Combo1");
dBox.requestFocus();

but the combobox doesn't get focus.

thanks
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Try an updateUI () call for your container (which contains the JComboBox) after requestFocus ().
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
zzynxSoftware engineerCommented:
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
 
CEHJCommented:
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
 
Mayank SAssociate Director - Product EngineeringCommented:
Can it not be done in a statement just after show () or setVisible ( true ) ?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
I mean - the updateUI () ?
0
 
RobCSPCommented:
Try it:


combo.requestFocusInWindow();


I hope that it  helps,

ROB
0
 
KennywenAuthor Commented:
i solve the problem by using coding below:

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

any disadvantage on above code?

thanks
0
 
CEHJCommented:
If what i first suggested is working, then stick with that
0
 
KennywenAuthor Commented:
sure i will but just want to know any disadvantage on it?
0
 
CEHJCommented:
Not having a deep enough knowledge of focus - i'd probably resort to trial and error myself ;-)
0
 
CEHJCommented:
8-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.