jtable按键盘上的下键就会从该行到下一行,回车也会换行,还有其它更多的键。
问题:有没有什么方法把所有的键事件全部屏蔽调,当我要的时候又启动。
给个代码例子,谢谢...
在线。。答对马上就结贴...
------解决方案--------------------
------解决方案--------------------
- Java code
addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e){ if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN){ e.consume(); } } .... // 其他方法});
------解决方案--------------------
这是测试类,
其实JTabel的那些键盘操作是通过
LazyActionMap 实现的,
不是通过KeyListener,
刚开始的回答有点想当然,不好意思了~
- Java code
import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.plaf.basic.BasicTableUI;@SuppressWarnings("serial")public class TableTest extends JFrame { public TableTest() { setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400, 300); setLocationRelativeTo(null); Object[][] data = new Object[][]{ {"121", "122", "123"}, {"321", "322", "323"}, {"441", "442", "443"}}; Object[] title = new Object[]{"1","2","3"}; JTable table = new JTable(data, title) { @Override public void updateUI() { super.updateUI(); setUI(new MyTableUI()); } }; setContentPane(new JScrollPane(table)); setVisible(true); } class MyTableUI extends BasicTableUI { @Override protected void installKeyboardActions() { } } public static void main(String... args) { new TableTest(); }}