Здравствуйте, я пытаюсь использовать TableRowSorter для фильтрации информации из Jtable, но я получаю эту ошибку: java.lang.ArrayIndexOutOfBoundsException.
InicializarCrudCos () заполняет таблицу, когда jframe начинает использовать LlenarTablaTrue ().
LlenarTablaTrue () использует уже созданный jtable в jframe, заполняет его (работает) и возвращает модель по умолчанию.
FiltrarDatos (), используемый для получения модели таблицы из LlenarTablaTrue () и установки сортировщика таблиц.
Вот код (удален некоторый код, который не используется для создания таблицы или ее фильтрации):
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)