我想在我的应用程序中的任何位置触发保存操作 (Control+S)。我已经添加了必要的键绑定(bind),并且操作按预期触发。但是,如果我在 JTable 上尝试 Control+S,则表格会启动我的自定义操作并激活表格单元格以进行编辑。我想我已经禁用了表格输入映射中的编辑操作。我在这里缺少什么?
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
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.UIManager;
public class TestTableKeyBinding extends JFrame{
JTable table;
JScrollPane scroll;
public static void main(String[] args){
TestTableKeyBinding test = new TestTableKeyBinding();
test.setVisible(true);
}
TestTableKeyBinding(){
super();
initUI();
addKeyBindings();
}
void initUI(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] headers = new String[]{"apples", "bananas"};
String[][] data = new String[][]{{"1", "2"},{"4","6"}};
table = new JTable(data, headers);
table.setCellSelectionEnabled(true);
scroll = new JScrollPane();
scroll.setViewportView(table);
this.add(scroll);
this.pack();
this.setSize(new Dimension(300, 400));
}
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("control S"), "save");
am.put("save", saveAction());
//disable table actions via 'none'
table.getInputMap().put(KeyStroke.getKeyStroke("control S"), "none");
table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("control S"), "none");
table.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke("control S"), "none");
table.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("control S"), "none");
((InputMap)UIManager.get("Table.ancestorInputMap")).put(KeyStroke.getKeyStroke("control S"), "none");
}
AbstractAction saveAction(){
AbstractAction save = new AbstractAction(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JOptionPane.showMessageDialog(TestTableKeyBinding.this.table, "Action Triggered.");
}
};
return save;
}
}
最佳答案
与@Guillaume 一样,我在运行您的代码时没有遇到任何问题。您可能会寻找一个无意中击败了 editingStopped()
的 CellEditor
,已讨论 here .一个sscce可能有助于澄清问题。
附录:您可以在 saveAction()
处理程序中取消编辑,如下所示。
table.editingCanceled(null);
作为引用,我在几个方面更新了您的示例:
- Control-S 未绑定(bind)到任何
JTable
Action
在常见的外观实现中,因此无需 remove它。 - 为了跨平台方便,使用
getMenuShortcutKeyMask()
. - Swing GUI 对象应该仅在 event dispatch thread 上构建和操作.
代码:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
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"}};
table = new JTable(data, headers);
table.setCellSelectionEnabled(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_S, MASK), "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);
}
};
return save;
}
}
关于java - JTable 键绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10573405/