vendredi 5 février 2021

My else statement on my comboBox state changed event is running while conditions are not met...Java

need some help here with my code. I have a combobox which in the initialization stage, its selectedIndex is set to -1 (empty). in my state changed listener for the combo box i have the code i will paste below. What its supposed to do is check if the selected index of the combobox is -1 then do nothing in my if statement, but if the index is not -1 it will print a row on my jTable with some information on it (this is my else statement) based on the selected index and then set the index back to -1 to deselect any items on my comboBox. It prints the information on m jTable just fine, but the problem is my else statement seems to run even when the selected index for my combobox is -1...this happens the first time i run the program. there is no other code which prints information on my jTable besides this code. please help

 private void jComboBoxItemNameItemStateChanged(java.awt.event.ItemEvent evt){                                                   
    boolean firstRun = true;
        if(evt.getStateChange() == ItemEvent.SELECTED){
           if(jComboBoxItemName.getSelectedIndex() == -1 && firstRun == true){
            //isMyComboBoxEmpty == true
      System.out.println("nothing");}
       else {
         try {
         addResultOnTable();
         jComboBoxItemName.setSelectedIndex(-1);
         //displaySearchOnTable();
        
     } catch (Exception ex) {
         System.out.println("comboBox is empty");
         Logger.getLogger(Inventory.class.getName()).log(Level.SEVERE, null, ex);
     }
    }
    }
    }

"

Aucun commentaire:

Enregistrer un commentaire