日期:2014-05-20 浏览次数:20629 次
addKeyListener(new KeyListener(){ public void keyPressed(KeyEvent e){ if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN){ e.consume(); } } .... // 其他方法 });
------解决方案--------------------
这是测试类,
其实JTabel的那些键盘操作是通过
LazyActionMap 实现的,
不是通过KeyListener,
刚开始的回答有点想当然,不好意思了~
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(); } }