mardi 2 mars 2021

How to delete the reflected row of the second JTable if the user selects another row in the first JTable in Java?

I am having trouble deleting the previous row of my second table of my Products Stock In GUI in Java.

User's first attempt of selecting a row

User selects another row

The flow of the functionality of my GUI is this:

  1. If the user will select a row in the first table of the GUI, the values of those rows will be reflected on the second rows

  2. If the user will select another/different row in the first table, the previous reflected row which was selected in the first table must be deleted and is replaced by the current selected row.

To sum it up, I have to replace/delete the previous reflected rows if I will select a different row in the first table then replace that previous selected row into the current selected row.

Here's my source code:

  private void firstTableMouseClicked(java.awt.event.MouseEvent evt) { 

    DefaultTableModel secondTblmodel = (DefaultTableModel) secondTable.getModel();
    int selectPRoww = firstTable.getSelectedRow();
      
    for(int x = 0; x < ProductAllData2[selectPRoww].length; x++){             
         if (ProductAllData2[selectPRoww][x][0] != null){ //ProductAllData2 is a 3D array
             if(setRowCountID2 == 0)
                   secondTblmodel.setRowCount(0);
                         
                 secondTblmodel.addRow(ProductAllData2[selectPRoww][x]); //Reflects and displays the values of the selected row from the first table to the second table
                 
                 if(selectPRoww > selectPRoww){   // deletes previous row if index of selected row is greater than the previous selected row (doesn't work)
                
                         secondTblmodel.removeRow(selectPRoww - 1);
                         
                     }
                 if (selectPRoww < selectPRoww){ // deletes previous row if index of selected row is less than the previous selected row (doesn't work)

                     secondTblmodel.removeRow(selectPRoww + 1);

                     }
                 
                  setRowCountID2++;

         }          
          
   }

Am I missing something in my functionality or do I need to modify the for loops?

Your guides would indeed help me a lot!! Thank you.

Aucun commentaire:

Enregistrer un commentaire