Java 如何从 MouseEvent 可靠地获取 JTable 中的行索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/243553/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:57:28  来源:igfitidea点击:

How to reliably get row index in JTable from MouseEvent?

javaswingjtable

提问by ?ukasz Bownik

How can I find out which row in a JTable the user just clicked?

如何找出用户刚刚单击的 JTable 中的哪一行?

采纳答案by Paul Tomblin

Try this:

尝试这个:

aJTable.rowAtPoint(evt.getPoint());

aJTable.rowAtPoint(evt.getPoint());

回答by oxbow_lakes

If you only ever care about listening to selections on the JTable:

如果您只关心听 JTable 上的选择:

jTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
    public void valueChanged(ListSelectionEvent e) {
        int sel = jTable.getSelectedRow();
    }
});