简体   繁体   English

通过JTable中的箭头键删除行选择

[英]remove row selection by arrow keys in jtable

I have implemented a JTable in JScrollpane. 我已经在JScrollpane中实现了JTable。 I have added MouseListener to the JTable, so its working fine with MouseListener. 我已经将MouseListener添加到JTable中,因此它可以与MouseListener一起正常工作。

Now my problem is, the row selection changes by the Keyboard Arrow keys(up arrow/down arrow) also and it does not call the methods that I have already implemented with MouseListener. 现在我的问题是,行选择也通过键盘上的箭头键(上箭头/下箭头)更改,并且它不会调用我已经使用MouseListener实现的方法。 So i just want to remove the JTable selection by Keyboard arrows. 所以我只想通过键盘箭头删除JTable选择。

Kindly help me. 请帮助我。

Swing components use Key Bindings to invoke Action when a give KeyStoke is entered. 输入give KeyStoke时,Swing组件将使用Key Bindings调用Action。

From reading the above tutorial link you should be able to remove the key bindings by using code like: 通过阅读上面的教程链接,您应该可以使用以下代码删除键绑定:

InputMap im = table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
im.put(KeyStroke.getKeyStroke("DOWN", 0), "none");

I'll let you handle the up key. 我让你处理向上键。

JTable has ListSelectionModel. JTable具有ListSelectionModel。 You can get it with method getSelectionModel(). 您可以使用getSelectionModel()方法获取它。 Then you can add ListSelectionListener to ListSelectionModel by method addListSelectionListener(ListSelectionListener). 然后,可以通过方法addListSelectionListener(ListSelectionListener)将ListSelectionListener添加到ListSelectionModel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM