• Post Reply Bookmark Topic Watch Topic
  • New Topic

JComboBox and keypress  RSS feed

 
Willem Kunkels
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have the following problem which I can't seem to figure out...

I have a JComboBox with a special model (second code snippet), which will first go to the database to get and sort Land objects (based on Locale).
A special renderer (third code snippet) will then render the items in the model depending on the Locale of the application (so, English users see info in English, French users in French, etc).

What I now want to do is allow users to press a key and then select the item in the display of which the first character starts with the pressed key. The default implementation of JComboBox is to use the underlying model to select items, but there are only Land objects in there that were used to render the display.

Is there a way to access the displayed information (ie. the rendered items) of the JComboBox?


From the model:


Finally, the snippet from the renderer:



Any help much appreciated,

Cheers,
Willem.
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A special renderer (third code snippet) will then render the items in the model depending on the Locale of the


You approach is wrong. You should not use a "locale renderer". You should just populate the combo box with the data from the locale. Then you won't have to worry about this.

Otherwise you would probably need to override the selectWithKeyChar() method of the combo box to implement your custom requirement.
 
Brian Cole
Author
Ranch Hand
Posts: 937
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick wrote:You approach is wrong. You should not use a "locale renderer". You should just populate the combo box with the data from the locale.

I can't quite agree that it's flat out "wrong." In general, too many people populate combo boxes with display strings instead of using renderers, so it's refreshing to see this locale renderer approach.

That said, in this case it's probably easier to populate the combo boxes with the localized strings. Or perhaps you could populate them with <localized, unlocalized> tuples.
 
Willem Kunkels
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All,

I have given this some thought and came up with a solution...

In the JComboBox's LisComboBoxModel (which is a subclass of DefaultComboBoxModel) I have introduced a Vector which holds the strings being displayed (based on the objects held in the model).
That Vector is initialized whenever the model is constructed or refreshed:

Also within the model I have introduced a key selection manager which I can then use to intercept the keystrokes and position the model through the items in the Vector holding the display strings...works like a charm! Wouldn't it be nice if we could just access the JList that holds the items being displayed...

Thanks for your help guys...

Cheers,
Willem
 
Rob Camick
Ranch Hand
Posts: 2756
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have introduced a Vector which holds the strings being displayed (based on the objects held in the model).
That Vector is initialized whenever the model is constructed or refreshed:


And that was exactly my point. This processing needs to be done every time the model is "constructed or refreshed", so just use the Vector "as the model" and you don't have to write any custom code.
 
Brian Cole
Author
Ranch Hand
Posts: 937
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me if your writing your own KeySelectionManager anyway, it could just call mlo.getString(config.getCurrentLocale()) directly instead of needed the displayItems list. Perhaps I'm missing something.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!