如何将键绑定设置为可编辑的JTable?

时间:2014-07-26 07:38:56

标签: java swing jtable

我想仅按JTable按钮删除Delete的选定行。

当我按Delete时,所选单元格变得可编辑,我的操作(在jframe上)不会收到KeyEvent

请运行此演示以查看效果:

public class TestTableKeyBinding extends JFrame {

    // private static final int MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    private JTable table;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TestTableKeyBinding test = new TestTableKeyBinding();
                test.setVisible(true);
            }
        });
    }

    TestTableKeyBinding() {
        super();
        initUI();
        addKeyBindings();
    }

    private void initUI() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] headers = new String[] { "apples", "bananas" };
        String[][] data = new String[][] { { "1", "2" }, { "4", "6" }, { "5", "7" }, { "1", "3" }, { "2", "11" } };
        table = new JTable(data, headers);
        table.setRowSelectionAllowed(true);     
        this.add(new JScrollPane(table));
        this.pack();
        this.setSize(new Dimension(300, 400));

    }

    private void addKeyBindings() {
        // root maps
        InputMap im = this.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        ActionMap am = this.getRootPane().getActionMap();
        // add custom action
        im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "save");
        am.put("save", saveAction());
    }

    private AbstractAction saveAction() {
        AbstractAction save = new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(TestTableKeyBinding.this.table, "Action Triggered.");
                table.editingCanceled(null);
                table.editingStopped(null);
                int selectedRow = table.getSelectedRow();
                if (selectedRow != -1) {
                    ((DefaultTableModel) table.getModel()).removeRow(selectedRow);
                }
            }
        };
        return save;
    }
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

我已经测试了您的代码并进行了一些更改,如下所述。现在它工作正常。

private void addKeyBindings() {
    KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);
    table.getActionMap().put("save", saveAction());
    table.getInputMap(JComponent.WHEN_FOCUSED).put(keyStroke, "save");
}

如下所述再做一次更改

table = new JTable(new DefaultTableModel(data,headers));

否则会导致ClassCastException位于以下行

((DefaultTableModel) table.getModel()).removeRow(selectedRow);

这是完整的代码:

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.table.DefaultTableModel;

public class TestTableKeyBinding extends JFrame {

    private JTable table;
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                TestTableKeyBinding test = new TestTableKeyBinding();
                test.setVisible(true);
            }
        });
    }

    TestTableKeyBinding() {
        super();
        initUI();
        addKeyBindings();
    }

    private void initUI() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        String[] headers = new String[] { "apples", "bananas" };
        String[][] data = new String[][] { { "1", "2" }, { "4", "6" }, { "5", "7" }, { "1", "3" },
                { "2", "11" } };
        table = new JTable(new DefaultTableModel(data,headers));
        table.setRowSelectionAllowed(true);
        this.add(new JScrollPane(table));
        this.pack();
        this.setSize(new Dimension(300, 400));

    }

    private void addKeyBindings() {
        KeyStroke keyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0);
        table.getActionMap().put("save", saveAction());
        table.getInputMap(JComponent.WHEN_FOCUSED).put(keyStroke, "save");
    }

    private AbstractAction saveAction() {
        AbstractAction save = new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(TestTableKeyBinding.this.table, "Action Triggered.");
                table.editingCanceled(null);
                table.editingStopped(null);
                int selectedRow = table.getSelectedRow();
                if (selectedRow != -1) {
                    ((DefaultTableModel) table.getModel()).removeRow(selectedRow);
                }
            }
        };
        return save;
    }
}