我想在jTable中没有选择任何行时禁用按钮。有什么可行的方法吗?

最佳答案

在JTable上使用SelectionListener

JTable table = new JTable();
JButton button = new JButton();
button.setEnabled(false);

ListSelectionModel listSelectionModel = table.getSelectionModel();
listSelectionModel.addListSelectionListener(new ListSelectionListener() {
        public void valueChanged(ListSelectionEvent e) {
            ListSelectionModel lsm = (ListSelectionModel)e.getSource();
            button.setEnabled(!lsm.isSelectionEmpty());
});

09-25 21:38