내 응용 프로그램 (Control + S)에서 저장 작업을 트리거하려고합니다. 필요한 키 바인딩을 추가하고 작업이 예상대로 트리거됩니다. 그러나 JTable에서 Ctrl + S를 누르면 테이블이 사용자 정의 액션을 시작하고 편집을 위해 테이블 셀을 활성화합니다. 테이블의 입력 맵에서 편집 작업을 비활성화 한 것 같습니다. 내가 여기서 무엇을 놓치고 있니?JTable 키 바인딩
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;
}
}
이 코드를 Win7 JDK1.6에서 실행하는 데 문제가 없습니다. –
@Guillaume 테이블 셀을 선택한 후 'Ctrl + S'를 누르면 문제가 발생합니다. 동작이 트리거되고 선택한 셀이 편집을 시작합니다. – kirbs