天天看點

java table删除標明行,如何在Java Swing的鍵事件中删除JTable中的標明行

java table删除標明行,如何在Java Swing的鍵事件中删除JTable中的標明行

I have to delete a selected row in JTable using the Key Event. When I select a row and press the Delete Key, the selected row values should be deleted. How can I do this?

解決方案

You have to get the selected Rows (thats where the curser currently is) and then call removeRow on that rows.

I recommend you read the API for JTable.

try this (I used multiple rows in the code where I used it, but you should be able to break it down to one. Also, I'm unsure if the Arrays.sort is really necessary)

int [] toDelete = dataTable.getSelectedRows();

Arrays.sort(toDelete); // be shure to have them in ascending order.

MyTableModel myTableModel = (MyTableModel)dataTable.getModel();

for(int ii = toDelete.length -1; ii >=0; ii--) {

myTableModel.removeRow(toDelete[ii]); // beginning at the largest.

}