Java 无法找出边界的数组索引异常:6>;1是什么意思?对jtable进行排序时
我在jtable排序机制中发现问题时遇到困难,当我在搜索代码中实现排序时,它会给我数组索引超出范围,填充表代码在初始阶段工作正常,但在搜索发生后,它也会被卡住 我的应用程序有一个文本字段区域,它接受从数据库中搜索的关键字。现在,在搜索之后,它会显示在jtable上,我想为它提供表分类器。感谢您的帮助。thks。我还提供了下面的堆栈跟踪 //问题代码Java 无法找出边界的数组索引异常:6>;1是什么意思?对jtable进行排序时,java,swing,jtable,tablemodel,tablerowsorter,Java,Swing,Jtable,Tablemodel,Tablerowsorter,我在jtable排序机制中发现问题时遇到困难,当我在搜索代码中实现排序时,它会给我数组索引超出范围,填充表代码在初始阶段工作正常,但在搜索发生后,它也会被卡住 我的应用程序有一个文本字段区域,它接受从数据库中搜索的关键字。现在,在搜索之后,它会显示在jtable上,我想为它提供表分类器。感谢您的帮助。thks。我还提供了下面的堆栈跟踪 //问题代码 // for searching data from data base and display it in table // according
// for searching data from data base and display it in table
// according to the keyword typed.
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
System.out.println(evt.getKeyCode());
if(evt.getKeyCode() == KeyEvent.VK_ENTER){
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null}
},
new String [] {
"cust_ID", "Title 2", "Title 3"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.removeColumn(jTable1.getColumnModel().getColumn(0));
String query = "SELECT emp_id, emp_name, emp_address FROM employee WHERE emp_name LIKE '"+jTextField1.getText()+"%' ";
ArrayList<String> s = new ArrayList<String>();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:EazySource");
Statement psmt = con.createStatement();
//smt.setChar(1,"a");
int rows = jTable1.getRowCount();
ResultSet result = psmt.executeQuery(query);
int i = 0;
DefaultTableModel model = null;
while(result.next()){
jTable1.getModel().setValueAt(Integer.toString(result.getInt("emp_id")), i, 0);
jTable1.setValueAt(result.getString("emp_name"), i, 0);
jTable1.setValueAt(result.getString("emp_address"), i, 1);
model = (DefaultTableModel)jTable1.getModel();
model.insertRow(jTable1.getRowCount(), new Object[]{null, null, null, null, null});
String s1 = jTable1.getModel().getValueAt(i, 0).toString();
System.out.println("id: "+s1);
i++;
}
model.removeRow(i);
TableRowSorter<TableModel> sorter
= new TableRowSorter<TableModel>(jTable1.getModel());
jTable1.setRowSorter(sorter);
for(String val:s){
jTextField1.setText(val);
}
con.close();
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}
}
}
//initially populating table when app starts
public void populatetable(){
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null}
},
new String [] {
"cust_ID", "Title 2", "Title 3"
}
) {
boolean[] canEdit = new boolean [] {
false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return canEdit [columnIndex];
}
});
jTable1.removeColumn(jTable1.getColumnModel().getColumn(0));
String query = "SELECT emp_id, emp_name, emp_address FROM employee ";
ArrayList<String> s = new ArrayList<String>();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:EazySource");
Statement psmt = con.createStatement();
//smt.setChar(1,"a");
int rows = jTable1.getRowCount();
ResultSet result = psmt.executeQuery(query);
int i = 0;
DefaultTableModel model = null;
while(result.next()){
jTable1.getModel().setValueAt(Integer.toString(result.getInt("emp_id")), i, 0);
jTable1.setValueAt(result.getString("emp_name"), i, 0);
jTable1.setValueAt(result.getString("emp_address"), i, 1);
model = (DefaultTableModel)jTable1.getModel();
model.insertRow(jTable1.getRowCount(), new Object[]{null, null, null, null, null});
String s1 = jTable1.getModel().getValueAt(i, 0).toString();
System.out.println("id: "+s1);
i++;
}
model.removeRow(i);
TableRowSorter<TableModel> sorter
= new TableRowSorter<TableModel>(jTable1.getModel());
jTable1.setRowSorter(sorter);
for(String val:s){
jTextField1.setText(val);
}
con.close();
}catch(Exception e){
System.out.println(e);
e.printStackTrace();
}
}
首先,请不要使用
KeyListener
,如果您想要这种操作,请使用ActionListener
-原因有100个,但现在请相信我们;)
在更改型号之前,尝试将分拣机设置为空
jTable1.setRowSorter(null);
我经常遇到这个问题,因为多个线程试图更新不同步的模型时发生了一个突变事件。挖掘一点(当我有时间和一些事情困扰我时总是这样:-)
首先:@MadProgrammer的回答是正确的-如果客户端代码负责保持分类器和模型本身的同步,而不是将该负担委托给表
承担责任的客户端代码:
TableModel model = .. //create some model;
table.setModel(model);
table.setRowSorter(new TableRowSorter(model));
// has to remove the rowSorter _before_ setting a new model
// because the old rowSorter still references the old model
table.setRowSorter(null);
TableModel newModel = ... // create some model ;
table.setModel(newModel);
table.setRowSorter(new TableRowSorter(newModel);
让桌子小心点:
table.setAutoCreateRowSorter();
TableModel model = .. //create some model;
table.setModel(model);
// nothing to fear, simply set the new
TableModel newModel = ... // create some model ;
table.setModel(newModel);
顺便说一句:在OP的上下文中,没有必要重置模型-它基本上是相同的,只是不同的数据。所以
// initially setting the model and rowSorter (in populate)
...
// in re-populating, instead of re-creating the same model
//table.setModel(newModel);
// clear
((DefaultTableModel) table.getModel()).setRowCount(0);
...
// and - while we are at it - cleanup the logic
while(result.next()) {
Object[] rowData = {result.getInt("emp_id"),
result.getString("emp_name"), result.getString("emp_address")};
((DefaultTableModel) jTable1.getModel()).addRow(rowData);
}
这意味着
向量中有1个元素,您试图在6.thks位置插入一个元素,得到了答案,只需将分类器设置为null。感谢MadProgrammer。非常感谢MadProgrammer,这个问题解决了。你说得对,我有变异问题。是的,从现在起我将尝试使用actionlistener。这是一个恼人的问题,我花了几个月的时间才找到它:Pya在过去的几天里,我也为此砸了好几次脑袋。:)@CluxR:您可以通过单击左侧的来接受这个答案。注意:这是一种黑客行为-应该可以与行为良好的线程一起工作。在生产代码中保留黑客是。。。次优:-)最可能的罪魁祸首是客户端代码试图在表的内部更新完成之前访问表。要进行检查,请尝试调用访问(而不是清空分类器)
// initially setting the model and rowSorter (in populate)
...
// in re-populating, instead of re-creating the same model
//table.setModel(newModel);
// clear
((DefaultTableModel) table.getModel()).setRowCount(0);
...
// and - while we are at it - cleanup the logic
while(result.next()) {
Object[] rowData = {result.getInt("emp_id"),
result.getString("emp_name"), result.getString("emp_address")};
((DefaultTableModel) jTable1.getModel()).addRow(rowData);
}