获取AbstractTableModel Java中选定行的值

获取AbstractTableModel Java中选定行的值,java,jtable,abstracttablemodel,Java,Jtable,Abstracttablemodel,我希望在AbstractTableModel中获得所选行的值,我注意到了一些事情。选中该行时,它会正确地报告我所在的销售行,但只要单击我的按钮删除,所选行值就会变为0。导致始终删除0行。我想获取int selectedRow值,并使用它将其从表和我的ArrayList中删除 ListSelectionModel rsm = table.getSelectionModel(); ListSelectionModel csm = table.getColumnModel().getSelection

我希望在AbstractTableModel中获得所选行的值,我注意到了一些事情。选中该行时,它会正确地报告我所在的销售行,但只要单击我的按钮删除,所选行值就会变为0。导致始终删除0行。我想获取int selectedRow值,并使用它将其从表和我的ArrayList中删除

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;
        }
      }
    }

TableModel与选择无关。ViewJTable负责选择

我想获取int selectedRow值,并使用它将其从表和我的ArrayList中删除

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);
您不应该有单独的ArrayList。数据应仅包含在TableModel中

如果要从表和表模型中删除行,则可以使用添加到删除按钮的ActionListener中的表的getSelectedIndex方法。比如:

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

如果您没有使用DefaultTableModel,那么您的自定义TableModel将需要实现removeRow。。。方法。

因此,这是显示选择列表的代码,但是。。。按钮的动作侦听器的代码在哪里?这是一个真正出错的方法。当我使用.getSelectedIndex时,我得到一个错误,即getSelectedIndex方法对于该类型是未定义的JTable@Ducksauce88你说得对,我打错了。阅读所有各种getSelected的JTable API。。。方法,我相信你会找到一个合适的方法。阅读API是了解哪些方法可以帮助您编写代码的好方法。