Eng*_*uad 2 java jtable keylistener
我知道我可以使用KeyListener来检查是否(char) 127按下了DELETE ,但是如何将keyListener添加到JTable中的selectedRow?
编辑:
我试过这个,但它不起作用:
myTable.addKeyListener(this);
...
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode() == 127 && myTable.GetSelectedRow() != -1)
{
btnRemove.doClick(); // this will remove the selected row in JTable
}
}
Run Code Online (Sandbox Code Playgroud)
Hov*_*els 11
KeyListeners的一个问题是被侦听的组件必须具有焦点.解决这个问题的一种方法是使用Key Bindings.
例如,
// assume JTable is named "table"
int condition = JComponent.WHEN_IN_FOCUSED_WINDOW;
InputMap inputMap = table.getInputMap(condition);
ActionMap actionMap = table.getActionMap();
// DELETE is a String constant that for me was defined as "Delete"
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), DELETE);
actionMap.put(DELETE, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// TODO: do deletion action here
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7412 次 |
| 最近记录: |