You should create an class that encapsulates this data including the item name and price, and then populate your JComboBox with objects of this class. e.g.,
public class MyItem {
private String itemName;
private double itemCost;
// any more fields?
public MyItem(String itemName, double itemCost) {
this. ///..... etc
}
// getters and setters
}
To have it appear nice, there's a quick and dirty way: give the class a toString()
method that prints out just the item name, e.g.,
@Override
public String toString() {
return itemName;
}
... or a more involved and probably cleaner way: give the JComboBox a renderer that shows only the item name.
Edit
You ask:
Ok, just unsure how I go about passing through the values from the file.
You would parse the file and create objects with the data. Pseudo code:
Create a Scanner that reads the file
while there is a new line to read
read the line from the file with the Scanner
split the line, perhaps using String#split(" ")
Get the name token and put it into the local String variable, name
Get the price String token, parse it to double, and place in the local double variable, price
Create a new MyItem object with the data above
Place the MyItem object into your JComboBox's model.
End of while loop
close the Scanner