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.
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.
The quieter you are, the more you are able to hear.
You got style baby! More than this tiny ad:
a bit of art, as a gift, the permaculture playing cards