2012-10-20 68 views
6

我有一個像下面的代碼。我想通過按刪除鍵刪除單元格。我怎樣才能做到這一點?而且我還想在該字段中添加刪除按鈕。如何通過按DELETE鍵從jTable中刪除單元格?

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

} 

這段代碼可能嗎?謝謝。

回答

6

您需要將刪除鍵綁定到表。

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

這僅僅是一個例子。只要你的表模型支持它,你可以用同樣的想法刪除整行。

+0

減去1的非功能性缺失/編譯器錯誤的'setValueAt' – BullyWiiPlaza

+0

@BullyWiiPlaza然後,我想你是唯一的一個。你遇到了什麼錯誤?我在許多生產系統中使用這個確切的代碼,所以你遇到的任何問題必須與你如何使用它 – MadProgrammer

+0

@BullyWiiPlaza所以,你基本上說「我有一個錯誤,所以它必須是其他人的錯! 「您不可能找到任何直接滿足您要求的答案,並且所提供的答案與問題的背景相關。但是,我始終使用這種類型的功能,通常刪除行 – MadProgrammer

0

在上面的例子中的最後一行應該是:

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

下面的代碼工作與排序JTable s表示在single selection模式。感謝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())); 
     } 
    } 
}); 
相關問題