首页 / JAVA / java – JTable密钥绑定
java – JTable密钥绑定
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – JTable密钥绑定,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4420字,纯文字阅读大概需要7分钟。
内容图文
![java – JTable密钥绑定](/upload/InfoBanner/zyjiaocheng/704/a49af73d8929487aba994f114be2d4cb.jpg)
我想在我的应用程序(Control S)的任何地方触发保存操作.我添加了必要的键绑定,操作按预期触发.但是,如果我在JTable上尝试控制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一样,我运行代码没有问题.你可能会寻找一个无意中击败editStopped()的CellEditor,讨论了here.一个sscce可能有助于澄清问题.
附录:您可以在saveAction()处理程序中取消编辑,如下所示.
table.editingCanceled(null);
作为参考,我在几个方面更新了您的示例:
> Control-S不受任何共同的JTable Action绑定.感觉实现,所以没有必要remove它.
>为了跨平台方便,请使用getMenuShortcutKeyMask().
>应仅在event dispatch thread上构造和操作Swing GUI对象.
码:
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密钥绑定全部内容,希望文章能够帮你解决java – JTable密钥绑定所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。