Link to home
Start Free TrialLog in
Avatar of Kennywen
Kennywen

asked on

Set focus on JComboBox

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

thanks
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Kennywen
Kennywen

ASKER

JComboBox dBox = new JComboBox(dLists);
dBox.addActionListener(this);
dBox.setActionCommand("Combo1");
dBox.requestFocus();

but the combobox doesn't get focus.

thanks
Try an updateUI () call for your container (which contains the JComboBox) after requestFocus ().
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
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
Can it not be done in a statement just after show () or setVisible ( true ) ?
I mean - the updateUI () ?
Try it:


combo.requestFocusInWindow();


I hope that it  helps,

ROB
i solve the problem by using coding below:

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

any disadvantage on above code?

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