如何在Java Swing的键事件中删除JTable中的选定行 [英] How to delete a selected row in JTable in Key Event in java swing

查看:920
本文介绍了如何在Java Swing的键事件中删除JTable中的选定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用键事件在JTable中删除选定的行.当我选择一行并按Delete键时,应删除所选的行值.我该怎么办?

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?

推荐答案

您必须获取选定的行(即光标当前所在的行),然后在该行上调用removeRow.

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

我建议您阅读 JTable的API

尝试一下(我在使用代码的地方在代码中使用了多行,但是您应该可以将其分解为一行.而且,我不确定Arrays.sort是否真的必要)

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

这篇关于如何在Java Swing的键事件中删除JTable中的选定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆