【问题标题】:How to delete a cell from jTable by pressing DELETE key?如何通过按 DELETE 键从 jTable 中删除单元格?
【发布时间】:2012-10-20 06:32:44
【问题描述】:

我有如下代码。我想通过按删除键来删除单元格。我怎样才能做到这一点?我还想在该字段中添加删除按钮。

private static final long serialVersionUID = -250883760398754970L;
private final LinkedList<Product> list= new LinkedList<Product>();
    private final LinkedList<Boolean> checkList = new LinkedList<Boolean>();
    public void addItem(Product customer) {
    list.add(customer);
    checkList.add(false);
    fireTableDataChanged();

}
@Override
public int getColumnCount() {
        return 6;
}

@Override
public int getRowCount() {
    return list.size();

}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Object obj = null;

    if(columnIndex==4)
       {
    setTotal(list.get(rowIndex));
    }
             switch (columnIndex){
        case 0: obj= list.get(rowIndex).getCode() ;break;
        case 1: obj=list.get(rowIndex).getDescription(); break;
        case 2: obj=list.get(rowIndex).getQuantity();break;
        case 3: obj=list.get(rowIndex).getPrice();break;            
        case 4: obj=list.get(rowIndex).getTotal();break;
    }
    return obj;
}
    @Override
public Class<?> getColumnClass(int arg0) {

    switch(arg0){
    case 0: case 1: return String.class; 
    case 2: return Integer.class; 
    case 3: case 4: return Double.class;
    }

    return super.getColumnClass(arg0);
}
@Override
public boolean isCellEditable(int arg0, int arg1) {
    boolean isCellEditable = false;
    switch(arg1){
    case 2: case 3: isCellEditable= true;break;
    default: isCellEditable= false;break;
    }
    return isCellEditable;
    //return super.isCellEditable(arg0, arg1);
}


@Override

public void setValueAt(Object arg0, int arg1, int arg2) {
    System.out.println("Value seted" +arg0 + arg1 + arg2);


    switch(arg2){
    case 0: break;
    case 1: break;
    case 2: list.get(arg1).setQuantity((Integer)arg0);             setTotal(list.get(arg1)); break;
    case 3: list.get(arg1).setPrice((Double)arg0); setTotal(list.get(arg1));break;          
    case 4: list.get(arg1).setTotal((Double)arg0);break;



       //case 0: checkList.set(arg1, (Boolean)arg0);break;
       default:break;
    }
    //list.get(arg1).setTotal((Double)arg0);
    fireTableDataChanged();
}



public LinkedList<Product> getList() {
    LinkedList<Product> temp = new LinkedList<Product>();
    int index=-1;
    for(Boolean isSelected:checkList){
        index++;
        if(isSelected){
            temp.add(list.get(index));
        }
    }
    return temp;
}


public void setTotal(Product product){
    Double total = 0.0d;
    total = product.getQuantity ()* product.getPrice();
    product.setTotal(total);

}

这段代码可以吗?谢谢。

【问题讨论】:

    标签: java swing jtable


    【解决方案1】:

    您需要将删除键绑定到表。

    查看How to use Key Bindings了解更多详情

    InputMap inputMap = table.getInputMap(WHEN_FOCUSED);
    ActionMap actionMap = table.getActionMap();
    
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "delete");
    actionMap.put("delete", new AbstractAction() {
        public void actionPerformed(ActionEvent evt) {
           // Note, you can use getSelectedRows() and/or getSelectedColumns
           // to get all the rows/columns that have being selected
           // and simply loop through them using the same method as
           // described below.
           // As is, it will only get the lead selection
           int row = table.getSelectedRow();
           int col = table.getSelectedColumn();
           if (row >= 0 && col >= 0) {
               row = table.convertRowIndexToModel(row);
               col = table.convertColumnIndexToModel(col);
               table.getModel().setValueAt(null, row, col);
           }
        }
    });
    

    这只是一个例子。只要您的表格模型支持,您可以使用相同的想法删除一整行。

    【讨论】:

    • 减 1 表示setValueAt 处的非功能性删除/编译器错误
    • @BullyWiiPlaza 那我猜你是唯一的一个。你遇到了什么错误?我在许多生产系统中都使用了这个确切的代码,所以您遇到的任何问题都必须与您使用它的方式有关
    • @BullyWiiPlaza 所以,你基本上是在说“我遇到了一个错误,所以这一定是其他人的错!”您不太可能找到任何直接完全满足您要求的答案,并且提供的答案与所提问题相关。但是,我一直使用这种类型的功能,通常是删除行
    • 报错如下:The method setValueAt(Object, int, int) in the type TableModel is not applicable for the arguments (int, int, null)。代码行应该是 table.getModel().setValueAt(null, row, col); 仍然喜欢你的贡献,MadProgrammer :)
    【解决方案2】:

    上例中的最后一行应该是:

    table.getModel().setValueAt(null, row, col);
    

    【讨论】:

      【解决方案3】:

      以下代码适用于处于single selection 模式的已排序JTables。感谢 MadProgrammer 的键绑定。

      table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
      
      InputMap inputMap = table.getInputMap(WHEN_FOCUSED);
      ActionMap actionMap = table.getActionMap();
      
      String deleteAction = "delete";
      inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0),
              deleteAction);
      actionMap.put(deleteAction, new AbstractAction()
      {
          public void actionPerformed(ActionEvent deleteEvent)
          {
              if (table.getSelectedRow() != -1)
              {
                  tableModel.removeRow(table.convertRowIndexToModel(table.getSelectedRow()));
              }
          }
      });
      

      【讨论】:

        猜你喜欢
        • 2011-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-11
        • 1970-01-01
        • 2011-12-22
        • 1970-01-01
        • 2015-06-30
        相关资源
        最近更新 更多