• 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 ...
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown

trying to read a map into a combo box

Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a project that requires me to read colors and Hex codes for the colors into a map from a text file. I have a TreeMap created and it stores and prints properly on the screen, but I can not figure out how to get it to send the data to a combo box. Here is my code thus far...

This compiles and runs, but i am trying to get the combo box to read the values imported instead of entering them, and the listener to read the elements instead of entering them.
Posts: 825
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello and welcome to the Ranch! Please read CarefullyChooseOneForum since this would be a topic for Swing forum.

Now, let's get to the point of the topic.
You should have your map as field inside your frame class. The code you have in your main method where you read colors.txt should be put as separate method, also inside your class (say, readColors(), or whatever name you come up with). Inside that method you would read your file as you did inside main, and put read values in your map. Afterwards, you can populate combo box with keys from your map, without initializing combo box with String array:

Now you can avoid all those if/else if you have in your ButtonListener:

Note just that if you want to have an empty string as one item in your combobox, you should map it to a default color (say white), otherwise you will get NullPointerException when user selects it, with the approach given above. Also, if you are using JDK 7 you can avoid multiple if/else-if since you can use a String as an expression in switch statement. You don't need it in this example, but keep that in mind in your future work.

At the end, I must notice that you don't really follow convention for naming variables.
You got style baby! More than this tiny ad:
a bit of art, as a gift, the permaculture playing cards
    Bookmark Topic Watch Topic
  • New Topic