JComboBox.setSelectedItem() issues

951 views Asked by At

My jTable Column 8 is the U.S. State column. This is my code to set the jComboBox with the appropriate item to display the state.

When I used Buffered Reader to fill the Jtable the below code worked really well to set the combobox item.

Because of parsing issues I decided to go with the default CSVReader library instead of rewriting it on my own. My jTable filling issues have been solved but now for some reason this code now longer works. Am I missing something?

jComboBox(0) is "--".

            java.lang.Object state = jTable1.getValueAt(jTable1.getSelectedRow(),8);
            String g = state.toString();

        if (g == "") {
            jComboBox1.setSelectedIndex(0);
        } else if (g == "AK") {
            jComboBox1.setSelectedIndex(1);
        } else if (g == "AL") {
            jComboBox1.setSelectedIndex(2);
        } else if (g == "AR") {
            jComboBox1.setSelectedIndex(3);
        }else if (g == "AZ") {
            jComboBox1.setSelectedIndex(4);
        }else if (g == "CA") {
            jComboBox1.setSelectedIndex(5);
        }else if (g == "CO") {
            jComboBox1.setSelectedIndex(6);
        }else if (g == "CT") {
            jComboBox1.setSelectedIndex(7);
        }else if (g == "DE") {
            jComboBox1.setSelectedIndex(8);
        }else if (g == "FL") {
            jComboBox1.setSelectedIndex(9);
        }else if (g == "GA") {
            jComboBox1.setSelectedIndex(10);
        }else if (g == "HI") {
            jComboBox1.setSelectedIndex(11);
        }else if (g == "IA") {
            jComboBox1.setSelectedIndex(12);
        }else if (g == "ID") {
            jComboBox1.setSelectedIndex(13);
        }else if (g == "IL") {
            jComboBox1.setSelectedIndex(14);
        }else if (g == "IN") {
            jComboBox1.setSelectedIndex(15);
        }else if (g == "KS") {
            jComboBox1.setSelectedIndex(16);
        }else if (g == "KY") {
            jComboBox1.setSelectedIndex(17);
        }else if (g == "LA") {
            jComboBox1.setSelectedIndex(18);
        }else if (g == "MA") {
            jComboBox1.setSelectedIndex(19);
        }else if (g == "MD") {
            jComboBox1.setSelectedIndex(20);
        }else if (g == "ME") {
            jComboBox1.setSelectedIndex(21);
        }else if (g == "MI") {
            jComboBox1.setSelectedIndex(22);
        }else if (g == "MN") {
            jComboBox1.setSelectedIndex(23);
        }else if (g == "MO") {
            jComboBox1.setSelectedIndex(24);
        }else if (g == "MS") {
            jComboBox1.setSelectedIndex(25);
        }else if (g == "MT") {
            jComboBox1.setSelectedIndex(26);
        }else if (g == "NC") {
            jComboBox1.setSelectedIndex(27);
        }else if (g == "ND") {
            jComboBox1.setSelectedIndex(28);
        }else if (g == "NE") {
            jComboBox1.setSelectedIndex(29);
        }else if (g == "NH") {
            jComboBox1.setSelectedIndex(30);
        }else if (g == "NJ") {
            jComboBox1.setSelectedIndex(31);
        }else if (g == "NM") {
            jComboBox1.setSelectedIndex(32);
        }else if (g == "NV") {
            jComboBox1.setSelectedIndex(33);
        }else if (g == "NY") {
            jComboBox1.setSelectedIndex(34);
        }else if (g == "OH") {
            jComboBox1.setSelectedIndex(35);
        }else if (g == "OK") {
            jComboBox1.setSelectedIndex(36);
        }else if (g == "OR") {
            jComboBox1.setSelectedIndex(37);
        }else if (g == "PA") {
            jComboBox1.setSelectedIndex(38);
        }else if (g == "RI") {
            jComboBox1.setSelectedIndex(39);
        }else if (g == "SC") {
            jComboBox1.setSelectedIndex(40);
        }else if (g == "SD") {
            jComboBox1.setSelectedIndex(41);
        }else if (g == "TN") {
            jComboBox1.setSelectedIndex(42);
        }else if (g == "TX") {
            jComboBox1.setSelectedIndex(43);
        }else if (g == "UT") {
            jComboBox1.setSelectedIndex(44);
        }else if (g == "VA") {
            jComboBox1.setSelectedIndex(45);
        }else if (g == "VT") {
            jComboBox1.setSelectedIndex(46);
        }else if (g == "WA") {
            jComboBox1.setSelectedIndex(47);
        }else if (g == "WI") {
            jComboBox1.setSelectedIndex(48);
        }else if (g == "WV") {
            jComboBox1.setSelectedIndex(49);
        }else if (g == "WY") {
            jComboBox1.setSelectedIndex(50);
        }

Thank you for you help. JB

1

There are 1 answers

6
Hovercraft Full Of Eels On

Don't compare Strings using ==. Use the equals(...) or the equalsIgnoreCase(...) method instead. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

if (fu == "bar") {
  // do something
}

do,

if ("bar".equals(fu)) {
  // do something
}

or,

if ("bar".equalsIgnoreCase(fu)) {
  // do something
}

Edit
By the way, your program design could be greatly improved. Why the use of magic numbers? Consider instead using enums for this purpose.