[英]Netbeans Java jTable gives error message without selection
我有一个jTable,里面装有来自MySQL数据库的数据。 如果用户选择一行并单击一个jButton,它将显示一个jDialog,她/他可以在其中编辑与所选行相关的所有数据。 它可以完美工作,但是当根本没有选择时,IDE会显示错误消息。 我能做什么?
错误信息:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
我的代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int username = 0;
int un_row = jTable1.getSelectedRow();
String uname = jTable1.getModel().getValueAt(un_row, username).toString();
if(morethanzero()) {
PersonalInfoEdit piedit = new PersonalInfoEdit(null, true, uname, perm);
piedit.setLocationRelativeTo(piedit);
piedit.setVisible(true);
}
}
boolean morethanzero(){
if(model.getRowCount() < 0 || model.getColumnCount() < 0) {
JOptionPane.showMessageDialog(null,"There's no selected user!","Error",JOptionPane.ERROR_MESSAGE);
jTable1.requestFocus();
return false;
}
return true;
}
先感谢您!
很简单,只需考虑是否未选择任何行。 例如,
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
int username = 0;
int un_row = jTable1.getSelectedRow();
if (un_row < 0) {
return; // if no row selected, get out!
}
// .... rest of code here
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.