Wyjątki rzucane przez listenera

0

Mam JTable, do którego podłączyłem model oraz ListSelectionListener, którego obsługa wygląda tak:

public void valueChanged(ListSelectionEvent e) {
	
			System.out.println(e.getFirstIndex()+"  "+e.getLastIndex());
			CarTableModel tabModel = (CarTableModel) tab.getModel();
			
			int rowIndex = tab.convertRowIndexToModel(tab.getSelectedRow()); //tłumaczenie indexu tabeli na index w modelu
			Car car = tabModel.getCar(rowIndex);
			
			tfNumber.setText(car.getNumber());
			tfAuto.setText(car.getModel());
			//...itd
		
	}

Rzuca wyjątkiem, kiedy majstruje coś na tabeli np. usuwam wiersz, ale z tym sobie poradziłem, bo podczas usuwania odłączam chwilowo listenera i przyłączam z powrotem. Nie wiem czy to eleganckie rozwiązanie ale działa.

Mam teraz podobny problem, bo kiedy mam coś zaznaczone w JTable i nacisnę na kolumnę, co sortuje tabelę wyrzucany jest wyjątek i nie wiem jak temu zapobiec. ListSelectionListener trochę głupieje przy sortowaniu. Co prawda program działa prawidłowo dalej (no prawie, ale mało to przeszkadza), ale to niedobrze, że gdzieś tam sobie rzuca wyjątkami.

Wyjątek:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
	at javax.swing.DefaultRowSorter.convertRowIndexToModel(Unknown Source)
	at javax.swing.JTable.convertRowIndexToModel(Unknown Source)
	at mainClass.Main.valueChanged(Main.java:355)
	at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
	at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
	at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
	at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
	at javax.swing.DefaultListSelectionModel.changeSelection(Unknown Source)
	at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(Unknown Source)
	at javax.swing.DefaultListSelectionModel.clearSelection(Unknown Source)
	at javax.swing.JTable.restoreSortingSelection(Unknown Source)
	at javax.swing.JTable.access$500(Unknown Source)
	at javax.swing.JTable$SortManager.restoreSelection(Unknown Source)
	at javax.swing.JTable$SortManager.processChange(Unknown Source)
	at javax.swing.JTable.sortedTableChanged(Unknown Source)
	at javax.swing.JTable.sorterChanged(Unknown Source)
	at javax.swing.RowSorter.fireRowSorterChanged(Unknown Source)
	at javax.swing.RowSorter.fireRowSorterChanged(Unknown Source)
	at javax.swing.DefaultRowSorter.sort(Unknown Source)
	at javax.swing.DefaultRowSorter.setSortKeys(Unknown Source)
	at javax.swing.DefaultRowSorter.toggleSortOrder(Unknown Source)
	at javax.swing.plaf.basic.BasicTableHeaderUI$MouseInputHandler.mouseClicked(Unknown Source)
	at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.processMouseEvent(Unknown Source)
	at java.awt.Component.processEvent(Unknown Source)
	at java.awt.Container.processEvent(Unknown Source)
	at java.awt.Component.dispatchEventImpl(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
	at java.awt.Container.dispatchEventImpl(Unknown Source)
	at java.awt.Window.dispatchEventImpl(Unknown Source)
	at java.awt.Component.dispatchEvent(Unknown Source)
	at java.awt.EventQueue.dispatchEvent(Unknown Source)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
	at java.awt.EventDispatchThread.run(Unknown Source) 

Problem oczywiście pojawia się dokładnie w tej linijce (335):
int rowIndex = tab.convertRowIndexToModel(tab.getSelectedRow());
Rozumiem, że podczas sortowania nie ma jak przetłumaczyć indexów i w tym wypadku nie jest to konieczne, wręcz nie chcę by listener reagował podczas sortowania tak jak podczas zaznaczenia rekodu.

1
public void valueChanged(ListSelectionEvent e) {
 
                        System.out.println(e.getFirstIndex()+"  "+e.getLastIndex());
                        CarTableModel tabModel = (CarTableModel) tab.getModel();
                        int n = tab.getSelectedRow();
                        if(n == -1) //nic nie jest zaznaczone
                        {
                             return;
                        }
                        int rowIndex = tab.convertRowIndexToModel(n); //tłumaczenie indexu tabeli na index w modelu
                        Car car = tabModel.getCar(rowIndex);
 
                        tfNumber.setText(car.getNumber());
                        tfAuto.setText(car.getModel());
                        //...itd
 
        }

I nie musisz odłączać listenera.

1 użytkowników online, w tym zalogowanych: 0, gości: 1