tags:

views:

175

answers:

2

Hello,

I would like to know how to set a JComboBox that contain integers values that I could save. Here is the definitions of values:

public class Item 
{
    private String itemDesc;
    private int itemType;

    public static int ENTREE=0;
    public static int MAIN_MEAL=1;
    public static int DESSERT=2;
    public static int DRINK=3;
    private float price;
    int[] itemTypeArray = { ENTREE, MAIN_MEAL, DESSERT, DRINK };
    Object[][] data = {{itemDesc, new Integer(itemType), new Float(price)}};
.
.
.
}

Now, I want the add a JComboBox that the user will choose 1 of the items (ENTREE, MAIN_MEAL...) and then I could set the number as an Integer.

I know that JComboBox need to be something like that:

JComboBox combo = new JComboBox(itemTypeArray.values());
        JOptionPane.showMessageDialog( null, combo,"Please Enter Item Type", `JOptionPane.QUESTION_MESSAGE);`

What am I doing wrong?

+2  A: 

The constructor for JComboBox askes for Object[]. But you cannot convert an int[]-array to an Integer[]-array. So you have to change your list to

Integer[] itemTypeArray = { ENTREE, MAIN_MEAL, DESSERT, DRINK };

Then it is possible to construct the combobox:

Item t = new Item();
JComboBox combo = new JComboBox(t.itemTypeArray);

But now you have the numbers in the list (0, 1, 2, 3). So just make a String-array:

String[] itemAliasArray = {"Entree", "Main meal", "Dessert", "Drink"};

Now you can construct the combobox like this:

Item t = new Item();
JComboBox combo = new JComboBox(t.itemAliasArray);

Now you have what you want (I think).


Notice that you are using a very strange design...

Martijn Courteaux
+1  A: 

Create Items as demonstrated in this posting

camickr