0

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Also, an interesting development. The comboBox is on the side pane with a bunch of textfield. The listSelectionEvent tells the program when a row is selected and the values are added to their specific text fields on the side. When the value in the text field is updated it is added to the selected row on the Jtable. When this row is later selected there is no issue with filling the jComboBox with the above code. It reads it perfectly. The openCSV.jar that i am now using must be the issue. Hope this helps. – Jake Brasch Oct 17 '12 at 04:36
  • 1
    What no longer works about it? – FThompson Oct 17 '12 at 04:37
  • Have to agree with @Vulcan -- you are leaving a lot of important details out of your question. – Hovercraft Full Of Eels Oct 17 '12 at 04:39
  • haha sorry, ur right. The above code no longer fills the jComboBox on the side – Jake Brasch Oct 17 '12 at 04:41
  • the jComboBox has 51 items. AK through WY (the 50 states) and a blank one "--" – Jake Brasch Oct 17 '12 at 04:43
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/) (but you will need to pretend that 47 states don't exist, to make it 'short'). – Andrew Thompson Oct 17 '12 at 07:32

1 Answers1

4

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373