我想在jTable中没有选择任何行时禁用按钮。有没有办法做到这一点?
答案 0 :(得分:6)
在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());
});
答案 1 :(得分:3)
这样的事情应该有效:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener()
{
@Override
public void valueChanged(ListSelectionEvent e)
{
if (!e.getValueIsAdjusting())
{
boolean rowsAreSelected = table.getSelectedRowCount() > 0;
button.setEnabled(rowsAreSelected);
}
}
});
答案 2 :(得分:0)
为您的表添加选择侦听器。如果出现选择,请启用该按钮。默认情况下禁用按钮。
http://download.oracle.com/javase/6/docs/api/javax/swing/JTable.html