Java JTable - 用于选择一行的 ActionListener
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20327005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JTable - ActionListener for select a row
提问by user3057184
I need the right AcionListener
for my JTable
.
我需要合适AcionListener
我的JTable
。
At program start there is no row selected by default. If I now select any row in this JTable
then the ActionListener
shall start.
在程序启动时,默认情况下不选择任何行。如果我现在在这个选择任何行JTable
,则ActionListener
应当启动。
回答by Paul Samsotha
Try this. I use a ListSelectionListener
and it works for me. I added a listener to the table Model
尝试这个。我使用 aListSelectionListener
并且它对我有用。我向表 Model 添加了一个侦听器
jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent event) {
if (jTable.getSelectedRow() > -1) {
// print first column value from selected row
System.out.println(jTable.getValueAt(jTable.getSelectedRow(), 0).toString());
}
}
});