Max Fortin
Max Fortin

Reputation: 125

JComboBox not returning expected result

public void actionPerformed(ActionEvent e) {
            String[] arr = {tf1.getText(),tf2.getText(),"","","","","","","","","","","","","","","",""};

            switch(cb1.getSelectedItem().toString()){

            case "Hillsborough":
                    arr[2]  = "1";
                    break;

            case "Pinellas":
                    System.out.println("HIT 1");
                    arr[3] = "1";
                    break;
            case "Pasco":
                    System.out.println("HIT 2");
                    arr[4]  = "1";
                    break;
            case "Hernando":
                    System.out.println("HIT 3");
                    arr[5]  = "1";
                    break;
            case "Polk":
                    System.out.println("HIT 3");
                    arr[6] = "1";
                    break;
            case "Manatee":
                    System.out.println("HIT 4");
                    arr[7]  = "1";
                    break;
            case "Sarasota":
                    System.out.println("HIT 5");
                    arr[8]  = "1";
                    break;
            case "Other Florida":
                    System.out.println("HIT 6");
                    arr[9]  = "1";
                    break;
            case "Other State/Country":
                    System.out.println("HIT 7");
                    arr[10]  = "1";
                    break;
            default:
                    break;
            }
            switch(cb2.getSelectedItem().toString()){
            case "Active Duty":
                    arr[11]  = "1";
                    break;
            case "Coalition Forces":
                    arr[12] = "1";
                    break;
            case "Dependent":
                    arr[13]  = "1";
                    break;
            case "Guard/Reserve":
                    arr[14]  = "1";
                    break;
            case "Retired":
                    arr[15] = "1";
                    break;
            case "Veteran":
                    arr[16]  = "1";
                    break;
            case "Civilian":
                    arr[17]  = "1";
                    break;

            default:
                    break;
            }
            reader.submit(2, arr);
            for(int c = 0; c < arr.length;c++)
                    System.out.print(arr[c]);
    }

When i click the confirm button it triggers this. It, depending on the selected result, will add a 1 to a specific spot in the array and the pass it on through another method. It has nothing to do with the second method, the results aren't being accessed at all. The first result in the combo box doesn't have a switch/case associated with it because those are the placeholders for the selction boxes. Any way to fix it?

Upvotes: 0

Views: 41

Answers (1)

Someone
Someone

Reputation: 61

And what return cbo.getSelectedItem().toString()?

  1. Use a List instead an array. List is dinamic so you can add elements when you need. Using a array when you don't know the length of data is unreadable and unnecessary.

    List<String> data = new ArrayList<>();
    data.add(cbo.getSelectedItem().toString());
    
  2. Are you sure you've added a String instead an Object? If you've added an Object, when you call to toString method you'll get the class and hashCode for it. So, if you've added an Object to your combo, you need override the toString method and get back an String (something that identify the Object).

    @Override
    public String toString() {
        return "some identifier";
    }
    

Upvotes: 1

Related Questions