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

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

JTable - ActionListener for select a row

javaswingjtableawtactionlistener

提问by user3057184

I need the right AcionListenerfor my JTable.

我需要合适AcionListener我的JTable

At program start there is no row selected by default. If I now select any row in this JTablethen the ActionListenershall start.

在程序启动时,默认情况下不选择任何行。如果我现在在这个选择任何行JTable,则ActionListener应当启动。

回答by Paul Samsotha

Try this. I use a ListSelectionListenerand 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());
        }
    }
});