I want to activate a button if I click an empty 8th column in my Jtable. But I'm getting this:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at records$1.valueChanged(records.java:57)
Here's my code:
tb_records.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
int row = tb_records.getSelectedRow();
DefaultTableModel model = (DefaultTableModel)tb_records.getModel();
String hehe = (String) model.getValueAt(row, 7);
if(!hehe.equals("")) {
b_extend.setEnabled(false);
}
else {
b_extend.setEnabled(true);
}
}
});
(not an answer, I'd be talking about simple way)
change ListSelectionMode
to SINGLE_....
, then only one row can be selected, otherwise you have to loop inside array of selected rows
test if (row > -1)
== no row is selected, if passed then you can to
for better help sooner post an SSCCE , short, runnable, compilable, with hardcoded value for JTable
I made few changes to your code, may be help.
.(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e) {
if (! e.getValueIsAdjusting() ) {
ListSelectionModel d = (ListSelectionModel)e.getSource();
if(d.getLeadSelectionIndex() != -1){
String hehe = (String) model.getValueAt(d.getLeadSelectionIndex(), 7);
b_extend.setEnabled("".equals(hehe));
}
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.