嗨,我正在尝试使用TableRowSorter从Jtable过滤信息,但出现此错误:java.lang.ArrayIndexOutOfBoundsException。
当jframe开始使用LlenarTablaTrue()时,InicializarCrudCos()填写表格。
LlenarTablaTrue()使用jframe中已经创建的jtable,并将其填充(它可以工作)并返回defaulttablemodel。
FiltrarDatos()用于从LlenarTablaTrue()获取表模型并设置tablerowsorter。
这是代码(删除了一些未用于创建表或对其进行过滤的代码):
public CentrocostosCRUD(CentrocostosDAO centrocostosCRUD, PanelLaboral_CentroCostos ventanaCentrocostos) {
this.centrocostosCRUD = centrocostosCRUD;
this.ventanaCentrocostos = ventanaCentrocostos;
this.ventanaCentrocostos.TFbusqueda.addKeyListener(this);
}
public void InicializarCrudCos() {
LlenarTablaTrue(ventanaCentrocostos.TablaCos);
//TablaCos is the name of the jtable in the jframe
}
public DefaultTableModel LlenarTablaTrue(JTable TablaUbi) {
DefaultTableModel modeloT = new DefaultTableModel();
TablaUbi.setModel(modeloT);
TablaUbi.setDefaultEditor(Object.class, null);
modeloT.addColumn("ID");
modeloT.addColumn("Centro de Costos");
modeloT.addColumn("Descripción");
modeloT.addColumn("Estado");
Object[] columna = new Object[4];
int numRegistros = centrocostosCRUD.listarTrue().size();
for (int i = 0; i < numRegistros; i++) {
columna[0] = centrocostosCRUD.listarTrue().get(i).getCos_id();
columna[1] = centrocostosCRUD.listarTrue().get(i).getCos_nombre();
columna[2] = centrocostosCRUD.listarTrue().get(i).getCos_descripcion();
String estado = "Habilitado";
columna[3] = estado;
modeloT.addRow(columna);
}
return modeloT;
}
public void FiltrarDatos(String query) {
DefaultTableModel model = LlenarTablaTrue(ventanaCentrocostos.TablaCos);
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(model);
ventanaCentrocostos.TablaCos.setRowSorter(tr);
tr.setRowFilter(RowFilter.regexFilter(query));
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getSource() == ventanaCentrocostos.TFbusqueda) {
String query = ventanaCentrocostos.TFbusqueda.getText().toLowerCase();
FiltrarDatos(query);
}
}
在用于搜索的文本框中键入(TFbusqueda)不会执行任何操作。
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at javax.swing.DefaultRowSorter.setModelToViewFromViewToModel(DefaultRowSorter.java:734)
at javax.swing.DefaultRowSorter.rowsInserted0(DefaultRowSorter.java:1063)
at javax.swing.DefaultRowSorter.rowsInserted(DefaultRowSorter.java:868)
at javax.swing.JTable.notifySorter(JTable.java:4270)
at javax.swing.JTable.sortedTableChanged(JTable.java:4118)
at javax.swing.JTable.tableChanged(JTable.java:4395)
at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:296)
at javax.swing.table.AbstractTableModel.fireTableRowsInserted(AbstractTableModel.java:231)
at javax.swing.table.DefaultTableModel.insertRow(DefaultTableModel.java:376)
at javax.swing.table.DefaultTableModel.addRow(DefaultTableModel.java:350)
at javax.swing.table.DefaultTableModel.addRow(DefaultTableModel.java:361)
at Controlador.CentrocostosCRUD.LlenarTablaTrue(CentrocostosCRUD.java:63)
at Controlador.CentrocostosCRUD.FiltrarDatos(CentrocostosCRUD.java:93)
at Controlador.CentrocostosCRUD.keyReleased(CentrocostosCRUD.java:131)
at java.awt.Component.processKeyEvent(Component.java:6494)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2832)
at java.awt.Component.processEvent(Component.java:6310)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
at java.awt.Component.dispatchEventImpl(Component.java:4760)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)