Hi I'm trying to use TableRowSorter to filter info from a Jtable, but I get this error: java.lang.ArrayIndexOutOfBoundsException.

InicializarCrudCos() fill the table when the jframe starts using LlenarTablaTrue().

LlenarTablaTrue() use the already created jtable in the jframe, and fills it (it works) and returns the defaulttablemodel.

FiltrarDatos() used to get the table model from LlenarTablaTrue() and set the tablerowsorter.

Here's the code (removed some code that is not used to create the table or filter it):

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);
        }
    }

Typing in the textbox used to search (TFbusqueda) does nothing.

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)

0 Answers