I want to disable a button whenever there aren't any rows selected in a jTable. Is there any possible way to do this?
Use a SelectionListener on your JTable.
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());
});
Something like this should work:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener()
{
@Override
public void valueChanged(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting())
{
boolean rowsAreSelected = table.getSelectedRowCount() > 0;
button.setEnabled(rowsAreSelected);
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With