2012-09-04 44 views
2

我需要從刪除鍵的一個JTable上刪除行。所以用例很簡單,用戶選擇一些行,按下delete鍵,行就會被刪除。該代碼也很簡單:JTable。刪除行。使事件不能進一步調度

DefaultTableModel tableModel = (DefaultTableModel) table.getModel(); 
    int[] selectedRows = table.getSelectedRows(); 
    for (int i = selectedRows.length - 1; i > -1; i--) { 
     tableModel.removeRow(selectedRows[i]); 
    } 

的問題是,後刪除被完成,我們將聽到蜂鳴聲(我在Windows上,典型的Windows蜂鳴聲),按在刪除鍵時,如空文本框(或當插入符號位於文本末尾時)。 我正在發生的事情是,按鍵被進一步分配給顯示單元格文本內容的文本組件(刪除的第一個單元格)。由DefaultEditorKit $ DeleteNextCharAction#actionPerformed方法觸發嘟嘟聲,因爲點之前沒有字符。 作爲一個黑客我修改的偵聽器的事件:

e.setKeyCode(KeyEvent.VK_SHIFT) // see JTable#processKeyBinding 

事件沒有得到進一步的轉發,蜂鳴dissapear,但我認爲這是一個壞的解決方案,並有一個更好的。但是,這是更好的解決方案?

+2

您是否嘗試過使用該事件? 'e.consume();'? –

+0

是的,這是正確的答案,謝謝 – Bax

回答

1

個使用鍵綁定,而不是...

InputMap im = table.getInputMap(JTable.WHEN_FOCUSED); 
ActionMap am = table.getActionMap(); 

im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "DeleteRow"); 
am.put("DeleteRow", new AbstractAction() { 
    @Override 
    public void actionPerformed(ActionEvent e) { 

     System.out.println("Delete row"); 
     int row = table.getSelectedRow(); 

     if (row > -1) { 

      DefaultTableModel model = (DefaultTableModel) table.getModel(); 
      model.removeRow(row); 

     } 

    } 
}); 

(我借mKorbel數據爲我的測試,所以我的測試是使用DefaultTableModel,你需要轉換到你所使用的型號)。

另外,如果你的編輯,這可能還是火,所以你需要檢查該

+0

是的,試了一下,它的工作原理,謝謝 – Bax

4
  • 您去除行核心是錯誤的,已經開始與最大的索引,否則刪除未選行,

代碼示例

import java.awt.*; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import javax.swing.table.*; 

public class RemoveAddRows extends JFrame { 

    private static final long serialVersionUID = 1L; 
    private Object[] columnNames = {"Type", "Company", "Shares", "Price"}; 
    private Object[][] data = { 
     {"Buy", "IBM", new Integer(1000), new Double(80.50)}, 
     {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)}, 
     {"Sell", "Apple", new Integer(3000), new Double(7.35)}, 
     {"Buy", "Nortel", new Integer(4000), new Double(20.00)}, 
     {"Buy", "IBM", new Integer(1000), new Double(80.50)}, 
     {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)}, 
     {"Sell", "Apple", new Integer(3000), new Double(7.35)}, 
     {"Buy", "Nortel", new Integer(4000), new Double(20.00)}, 
     {"Buy", "IBM", new Integer(1000), new Double(80.50)}, 
     {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)}, 
     {"Sell", "Apple", new Integer(3000), new Double(7.35)}, 
     {"Buy", "Nortel", new Integer(4000), new Double(20.00)} 
    }; 
    private JTable table; 
    private DefaultTableModel model; 

    public RemoveAddRows() { 
     model = new DefaultTableModel(data, columnNames) { 

      private static final long serialVersionUID = 1L; 

      @Override 
      public Class getColumnClass(int column) { 
       return getValueAt(0, column).getClass(); 
      } 
     }; 
     table = new JTable(model); 
     table.setPreferredScrollableViewportSize(table.getPreferredSize()); 
     table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 
     JScrollPane scrollPane = new JScrollPane(table); 
     add(scrollPane); 
     JButton button1 = new JButton("Remove all rows"); 
     button1.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent arg0) { 
       int[] selectedRows = table.getSelectedRows(); 
       for (int i = selectedRows.length - 1; i > -1; i--) { 
        model.removeRow(selectedRows[i]); 
       } 
       /*if (model.getRowCount() > 0) { 
       for (int i = model.getRowCount() - 1; i > -1; i--) { 
       model.removeRow(i); 
       } 
       }*/ 
       System.out.println("model.getRowCount() --->" + model.getRowCount()); 
      } 
     }); 
     JButton button2 = new JButton("Add new rows"); 
     button2.addActionListener(new ActionListener() { 

      public void actionPerformed(ActionEvent arg0) { 
       Object[] data0 = {"Buy", "IBM", new Integer(1000), new Double(80.50)}; 
       model.addRow(data0); 
       Object[] data1 = {"Sell", "MicroSoft", new Integer(2000), new Double(6.25)}; 
       model.addRow(data1); 
       Object[] data2 = {"Sell", "Apple", new Integer(3000), new Double(7.35)}; 
       model.addRow(data2); 
       Object[] data3 = {"Buy", "Nortel", new Integer(4000), new Double(20.00)}; 
       model.addRow(data3); 
       System.out.println("model.getRowCount() --->" + model.getRowCount()); 
      } 
     }); 
     JPanel southPanel = new JPanel(); 
     southPanel.add(button1); 
     southPanel.add(button2); 
     add(southPanel, BorderLayout.SOUTH); 
    } 

    public static void main(String[] args) { 
     RemoveAddRows frame = new RemoveAddRows(); 
     frame.setDefaultCloseOperation(EXIT_ON_CLOSE); 
     frame.pack(); 
     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
} 
  • 我贏得用戶的永不聽到來自Win平臺的嗶聲,必須發帖SSCCE
+0

這是一個連續的選擇每次(sry,忘記告訴),無論如何,消費()方法這是正確的答案 – Bax

+0

改變了問題,以包含您的版本刪除,因爲它是更好的 – Bax

+0

沒有任何理由消耗()任何鼠標或鍵盤事件,以更好地幫助更快地發佈[SSCCE](http://sscce.org/)生成的輸出到PC的揚聲器 – mKorbel

0

這是MadProgrammer's代碼,但我修改它由一個一次,而不是一個具有多個選定的行工作:

// Assume table is a JTable instance 
    InputMap inputMap = table.getInputMap(JTable.WHEN_FOCUSED); 
    ActionMap actionMap = table.getActionMap(); 

    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "DeleteRow"); 
    actionMap.put("DeleteRow", new AbstractAction() 
    { 
     @Override 
     public void actionPerformed(ActionEvent e) 
     { 
      int[] row = table.getSelectedRows(); 

      for (int i = 0; i < row.length; i++) 
      { 
       ((DefaultTableModel) table.getModel()).removeRow(row[i] - i * 1); 
      } 
     } 
    });