1

enter image description here

I was working on creating a weather application in Java using Weather Underground and I found that it does have data for some cities.

Initially, I was planning on using GeopIP to get the user's location automatically but since the support for cities is limited I decided to let the user choose the city every time the program starts.

I want the user to be able to choose a city from one that is supported by Weather Underground. The user will enter the name and as he/she enters the name, the possible locations will be displayed in a way similar to the one shown in the picture.

My question is:

  • How do I implement this search feature ?
  • My initial guess was to create a Vector containing all the names of the cities and then use brute force to find the match and display in a JPopup or a JWindow containing a JList but I guess there has to be a better method

    Rephrase:

    What I do not understand is WHAT INFO do I keep in the data structure I must use ? Should I manually create a list of cities that Weather Underground supports or is there another way to do it ?

    An SO User
    • 24,612
    • 35
    • 133
    • 221
    • No, that's pretty much exactly how you do it (with the exception of don't use `Vector`s): http://stackoverflow.com/questions/7255636/is-it-possible-to-have-an-autocomplete-using-jtextfield-and-a-jlist/7255918#7255918 (among numerous others) – Brian Roach Feb 12 '13 at 18:10
    • 1
      Concrete code here http://stackoverflow.com/questions/8476223/windows-application-with-auto-complete-using-tab-of-unix-machine-files-and-direc. – Joop Eggen Feb 12 '13 at 18:14

    2 Answers2

    3

    Take a look at the Trie data structure (also known as digital tree or prefix tree). Autocompletion is one of the most common examples of it's usefulness.

    The following article has a nice an very approachable explanation:

    Roll your own autocomplete solution using Tries.

    Marcelo
    • 2,232
    • 3
    • 22
    • 31
    1

    if you google autosuggestcombobox you will get some interesting results:

    This one is written in JavaFX - I have used and extended it myself already. It is quite useful. What you get "for free" with JavaFX: a context menu with right-mouse click which is auto-generated containing some of the usual "stuff", like cut, copy & paste and even undo! So, I can recommend that solution. To get into JavaFX isn't so hard - and I think it is much easier to learn than Swing - and looks so much cooler! However this implementation has some drawbacks - especially when the layout is not left-aligned, because it is simply a text field on top of a combobox.

    OK - but if you want to stick to Swing - you could probably use this one. I haven't used that myself, but the code looks quite straightforward and pretty clean - cleaner than the implementation for JavaFX I must admit (but that had some nice features). So - maybe you try - and extend it? It is built simply on JComboBox.

    michael_s
    • 2,515
    • 18
    • 24