Skip to main content

setLabelFor and JComboBox

1 reply [Last post]
kyle
Offline
Joined: 2003-06-11

Does setLabelFor work if JComboBox is the target? I want to set a mnemonic in the label, and have the mnemonic be "for" the JComboBox.

For JTextFields, I've found it works most predictably if I add a focusListener on the label, so that if the label gains focus, it automatically tells the JTextField to requestFocus. It seems like setLabelFor should handle that automatically, but it worked only about half the time; with a focusListener it works all the time.

However, with JComboBox as the target I can't get either method to work. The mnemonic in the label doesn't do anything.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kyle
Offline
Joined: 2003-06-11

I hadn't pasted my code in to this message, after I was about to, I saw the problem. Although the variable for the combobox was around, it was not initialized at the time I called setLabelFor. Didn't create the actual combobox till later.

Now that I create the combobox before calling setLabelFor -- it's the same behavior as JTextField. It works kind of sporadically until I add a focusListener to the label that does requestFocus for the comboBox; when I do that, it acts as I would expect it to.