Java 检索JTable中JCheckbox的状态
由于某些原因,我无法找出我的代码有什么问题。基本上Java 检索JTable中JCheckbox的状态,java,swing,Java,Swing,由于某些原因,我无法找出我的代码有什么问题。基本上 在这种情况下,我将计数器(总数)初始化为2 每次选中JCheckbox(在本例中为4)并在单击过多时抛出消息时,增量最大为6 首先,当检查总共6个时,它抛出空指针;但是,如果选中了6个以上的复选框,然后单击多个按钮将其减少到6个,则可以正常工作 有人能告诉我它在哪里扰乱我的头脑吗?多谢各位 public void panelThree(JTable user, JButton save, JButton logout, JButton exit
JCheckbox
(在本例中为4)并在单击过多时抛出消息时,增量最大为6public void panelThree(JTable user, JButton save, JButton logout, JButton exit, int j) {
System.out.println("J in external main is: " + j);
save.addActionListener(new ActionListener() {
boolean arr[] = new boolean[user.getRowCount()];
public void actionPerformed(ActionEvent e) {
int total = j;
System.out.println("Total in listener is: "+total);
System.out.println("No of rows: " + user.getRowCount());
for (int z = 0; z < arr.length; z++) {
arr[z] = false;
}
for (int i = 0; i < user.getRowCount(); i++) {
if (Boolean.valueOf(user.getModel().getValueAt(i, 3).toString()) == true) {
if (arr[i] == false) {
arr[i] = true;
total++;
}
} else {
if (arr[i] == true) {
arr[i] = false;
total--;
}
}
}
System.out.println("Total is: " + total);
if (total > 6) {
JOptionPane.showMessageDialog(null, "Please choose up to a maximum of " + (6 - j) + " modules");
} else {
int reply = JOptionPane.showConfirmDialog(null, "Are you sure to enroll in these modules?", "Yes", JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null, "Enrollment Successful");
}
}
}
});
}
public void panel三(JTable user、JButton save、JButton logout、JButton exit、int j){
System.out.println(“外部干管中的J为:“+J”);
save.addActionListener(新建ActionListener()){
布尔arr[]=新布尔值[user.getRowCount()];
已执行的公共无效操作(操作事件e){
int-total=j;
System.out.println(“侦听器中的总计为:“+Total”);
System.out.println(“行数:+user.getRowCount());
对于(int z=0;z6){
showMessageDialog(null,“请最多选择”+(6-j)+“模块”);
}否则{
int reply=JOptionPane.showConfirmDialog(null,“您确定要注册这些模块吗?”,“是”,JOptionPane.Yes\u NO\u选项);
if(reply==JOptionPane.YES\u选项){
showMessageDialog(null,“注册成功”);
}
}
}
});
}
行
if(Boolean.valueOf(user.getModel().getValueAt(i,3.toString())==true)
似乎是所有错误的原因。我正在进行的所有编辑控制台都显示该行的空指针您的表似乎包含空值。所以你需要在你的支票上考虑一下。大概是这样的:
if (user.getModel().getValueAt(i, 3) == Boolean.TRUE)
如果我的建议不起作用,请提供一个链接,以便我们也可以重现您的问题。@Mr.Polywhirl它与SSCCE相同,但链接较短:)。@Sergiymedvynsky这种结构看起来更好吗?@Sergiymedvynsky先生。。。你是救世主。。。非常好,非常感谢:我已经更正了我的答案