0

I am looking to get the value of the selected row in an AbstractTableModel and I am noticing some things. It is correctly reporting what sell (row) I am on, when it is selected, but as soon as I click my button to remove, the selected row value goes to 0. Resulting in the 0 row always being removed. I want to get the value int selectedRow and use it to remove it from the table and my ArrayLists.

ListSelectionModel rsm = table.getSelectionModel();
ListSelectionModel csm = table.getColumnModel().getSelectionModel();
csm.addListSelectionListener(new SelectionDebugger(columnCounter,csm));

columnCounter = new JLabel("(Selected Column Indices Go Here)");
columnCounter.setBounds(133, 62, 214, 14);
csm.addListSelectionListener(new SelectionDebugger(columnCounter,csm));
contentPane1.add(columnCounter);

rowCounter = new JLabel("(Selected Column Indices Go Here)");
rowCounter.setBounds(133, 36, 214, 14);
rsm.addListSelectionListener(new SelectionDebugger(rowCounter, rsm));
contentPane1.add(rowCounter);

SelectionDebugger:

public class SelectionDebugger implements ListSelectionListener {
        JLabel debugger;
        ListSelectionModel model;

        public SelectionDebugger(JLabel target, ListSelectionModel lsm) {
          debugger = target;
          model = lsm;
        }
        public void valueChanged(ListSelectionEvent lse) {
          if (!lse.getValueIsAdjusting()) {
            // skip all the intermediate events . . .
            StringBuffer buf = new StringBuffer();
            int[] selection = getSelectedIndices(model.getMinSelectionIndex(),
                                                 model.getMaxSelectionIndex());
            if (selection.length == 0) {
              buf.append("none");
              //selectedRow = buf.toString();
            }
            else {
              for (int i = 0; i < selection.length -1; i++) {
                buf.append(selection[i]);
                buf.append(", ");
              }
              buf.append(selection[selection.length - 1]);
            }
            debugger.setText(buf.toString());
            System.out.println("CampaignConfiguration: Selected Row: " + selection[selection.length - 1]);
            // Set the selected row for removal;
            selectedRow = selection[selection.length - 1];
          }
        }

        // This method returns an array of selected indices. It's guaranteed to
        // return a nonnull value.
        protected int[] getSelectedIndices(int start, int stop) {
          if ((start == -1) || (stop == -1)) {
            // no selection, so return an empty array
            return new int[0];
          }

          int guesses[] = new int[stop - start + 1];
          int index = 0;
          // manually walk through these . . .
          for (int i = start; i <= stop; i++) {
            if (model.isSelectedIndex(i)) {
              guesses[index++] = i;
            }
          }

          // ok, pare down the guess array to the real thing
          int realthing[] = new int[index];
          System.arraycopy(guesses, 0, realthing, 0, index);
          return realthing;
        }
      }
    }
Ducksauce88
  • 640
  • 3
  • 12
  • 26
  • So, this is the code where you show the list of selections, but... where is the code of the button's action listener? That's the one that actually goes wrong. – RealSkeptic Mar 24 '15 at 20:21

1 Answers1

0

The TableModel has nothing to do with selection. The View(JTable) is responsible for the selection.

I want to get the value int selectedRow and use it to remove it from the table and my ArrayLists.

You should NOT have separate ArrayLists. The data should only be contained in the TableModel.

If you want to delete a row from the table (and the TableModel) then you can use the getSelectedIndex() method of the table in your ActionListener added to the "Delete" button. Something like:

int row = table.getSelectedIndex();
if (row != -1)
{
    int modelRow = table.convertRowIndexToModel( row );
    tableModel.removeRow( modelRow );
}

If you are not using the DefaultTableModel, then your custom TableModel will need to implement the "removeRow(...)" method.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • When I use `.getSelectedIndex()` I get the error `The method getSelectedIndex() is undefined for the type JTable` – Ducksauce88 Mar 25 '15 at 13:45
  • @Ducksauce88 you are correct, I made a typo. Read the JTable API for all the various `getSelected...()` methods and I'm sure you will find one that is appropriate. Reading the API is a good way to learn what methods are available to help you in your coding. – camickr Mar 25 '15 at 14:44