• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JComboBox and keypress

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Would you like to try a free sample? Today we are featuring tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic