This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes trying to read a map into a combo box Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "trying to read a map into a combo box" Watch "trying to read a map into a combo box" New topic
Author

trying to read a map into a combo box

Larry Burden
Greenhorn

Joined: Oct 13, 2012
Posts: 1
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.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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.


The quieter you are, the more you are able to hear.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trying to read a map into a combo box
 
Similar Threads
Assignment Log
parsing in Xalan
HTML Horizontal Line
Generating list of colours.
Regex vs. String[]