How to delete a selected row in JTable in Key Event in java swing [closed]

Deadly 提交于 2019-12-13 11:11:45

问题


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?


回答1:


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.
}


来源:https://stackoverflow.com/questions/10135210/how-to-delete-a-selected-row-in-jtable-in-key-event-in-java-swing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!