Java – JTable – actionlistener for selecting rows

My JTable needs the correct acionlister

When the program starts, no rows are selected by default If I select any row in this JTable now, the actionlistener will start

Solution

Try this I use listselectionlistener, which applies to me I added a listener to the table 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());
        }
    }
});
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>