在AbstractTableModel Java中获取所选行的值

时间:2015-03-24 20:07:43

标签: java jtable abstracttablemodel

我希望在AbstractTableModel中获取所选行的值,我注意到了一些事情。它正确地报告了我所选择的卖(行),当它被选中时,但是当我单击我的按钮删除时,所选行值变为0.导致0行始终被删除。我想获取值selectedRow并使用它从表和我的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;
        }
      }
    }

1 个答案:

答案 0 :(得分:0)

TableModel与选择无关。 View(JTable)负责选择。

  

我想获取int selectedRow的值并使用它从表和我的ArrayLists中删除它。

您不应该有单独的ArrayLists。数据应仅包含在TableModel中。

如果要从表(和TableModel)中删除一行,则可以使用添加到“删除”按钮的ActionListener中表的getSelectedIndex()方法。类似的东西:

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

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