Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: TableRowSorter throws Exception

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Location
    İstanbul
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default TableRowSorter throws Exception

    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);
    Last edited by Onur; September 6th, 2011 at 03:54 AM.


  2. #2
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: TableRowSorter throws Exception

    Thanks your post helped me a lot

Similar Threads

  1. Executing commands throws an InterruptedException even with waitFor()
    By LilaH in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 20th, 2011, 03:57 AM
  2. Singly Linked List of Integers, get(int i) function throws Null Pointer Exception
    By felixtum2010 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 06:55 PM
  3. Replies: 6
    Last Post: March 25th, 2011, 03:42 PM
  4. ZipInputStream Throws Illegalargument exception for diacritics
    By meghaladevi in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: October 12th, 2010, 02:32 AM
  5. [SOLVED] File.createTempFile() throws exception on Win7
    By jitinsingla in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 10th, 2010, 02:14 PM