Hi,
I created an application that use jtable to display 650 record from an excel file. And i added TableRowSorter for filtering and sorting the data. To use filtering i used jtextfield and jbutton, when the user write a keyword into a textfiled and press search button, it filters all rows and columns. This part works without any error. The code here:
else if( e.getActionCommand().equals("Search")){ //selectionModel.clearSelection(); String text = filterField.getText(); if( text.trim().length() == 0) //if nothing entered to textfield return null sorter.setRowFilter(null); else sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text)); // "(?i)" makes search case-insensitive }
Than, i want to get selected data from selected row and specific column( column = 0 ). I think the problem occured because of this code. I used this:
public void valueChanged(ListSelectionEvent e) { selection = table.getValueAt(table.getSelectedRow(), 0); }
After i added this code, it works normally until i select any row from the table. If i don't select any row, it do filtering. But if i select any row from the table it throws an exception, and not filtering.
The exception is :
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1 at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:501) at javax.swing.JTable.convertRowIndexToModel(JTable.java:2611) at javax.swing.JTable.getValueAt(JTable.java:2686) at deneme.valueChanged(deneme.java:299) at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167) at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147) at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194) at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:388) at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:398) at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:559) at javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:403) at javax.swing.JTable$SortManager.restoreSelection(JTable.java:4017) at javax.swing.JTable$SortManager.processChange(JTable.java:3985) at javax.swing.JTable.sortedTableChanged(JTable.java:4117) at javax.swing.JTable.sorterChanged(JTable.java:3807) at javax.swing.RowSorter.fireRowSorterChanged(RowSorter.java:323) at javax.swing.RowSorter.fireRowSorterChanged(RowSorter.java:315) at javax.swing.DefaultRowSorter.sort(DefaultRowSorter.java:595) at javax.swing.DefaultRowSorter.setRowFilter(DefaultRowSorter.java:407) at deneme.actionPerformed(deneme.java:337) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6288) at javax.swing.JComponent.processMouseEvent(JComponent.java:3267) at java.awt.Component.processEvent(Component.java:6053) at java.awt.Container.processEvent(Container.java:2041) at java.awt.Component.dispatchEventImpl(Component.java:4651) at java.awt.Container.dispatchEventImpl(Container.java:2099) at java.awt.Component.dispatchEvent(Component.java:4481) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168) at java.awt.Container.dispatchEventImpl(Container.java:2085) at java.awt.Window.dispatchEventImpl(Window.java:2478) at java.awt.Component.dispatchEvent(Component.java:4481) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643) at java.awt.EventQueue.access$000(EventQueue.java:84) at java.awt.EventQueue$1.run(EventQueue.java:602) at java.awt.EventQueue$1.run(EventQueue.java:600) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98) at java.awt.EventQueue$2.run(EventQueue.java:616) at java.awt.EventQueue$2.run(EventQueue.java:614) at java.security.AccessController.doPrivileged(Native Method) at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) at java.awt.EventQueue.dispatchEvent(EventQueue.java:613) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I thought a solution: Before searching i tried to clear selection from the table, but it didn't work. I commented it in "Search" action, as you can see.
The GUI looks like this:
Edit: I forgot to give my table code
MyDefaultTableModel tableModel = new MyDefaultTableModel(e.readRows(path),title); table = new JTable(tableModel); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); selectionModel = table.getSelectionModel(); selectionModel.addListSelectionListener(this); sorter = new TableRowSorter<TableModel>(table.getModel()); //this is for sorting and filtering table.setRowSorter(sorter);
import javax.swing.table.*; import javax.swing.*; import java.util.*; class MyDefaultTableModel extends DefaultTableModel { public MyDefaultTableModel(Object[][] data, Object[] columnNames) { super(data, columnNames); } //disable editing, default is true public boolean isCellEditable(int row, int col) { return false; } }
Any suggestion to this problem ?
EDIT 2: I solved the problem This is always happening, when you write some problems and than you see the solution
The problem Reason : "getSelectedRow()" returns -1 if no row is selected. Than "table.getValueAt(table.getSelectedRow(), 0);" gives "java.lang. Arra yIndexOutOfBoundsException: -1". I simply put this:
if(table.getSelectedRow() != -1) selection = table.getValueAt(table.getSelectedRow(), 0);