且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JTable中。删除行。消耗事件不要进一步调度

更新时间:2023-12-04 14:32:49

使用键绑定代替......

Use key bindings instead...

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 ,你需要转换为你正在使用的模型。

(I borrowed mKorbel data for my test, so my test was using a DefaultTableModel, you will need to cast to the model you are using).

另外,如果你编辑,这可能仍然会触发,所以你将需要检查

Also, if you editing, this may still fire, so you will need to check for that