[英]NullPointerException in Clicking a row in JTable
I want to activate a button if I click an empty 8th column in my Jtable. 如果我在Jtable中单击一个空的第8列,我想激活一个按钮。 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
将
ListSelectionMode
更改为SINGLE_....
,则只能选择一行,否则必须在array of selected rows
内循环
test if (row > -1)
== no row is selected, if passed then you can to 测试
if (row > -1)
==未选择任何行,如果通过,则可以
for better help sooner post an SSCCE , short, runnable, compilable, with hardcoded value for JTable 为了更好地提供帮助,请尽快发布SSCCE ,short,可运行,可编译的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));
}
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.