Question

I have a jtable that extends AbstractTableModel. I registered two ListSelectionListeners which get the row and column of the selected cell. However, when I called the updateData() method defined in the model which in turn invokes fireTableDataChanged() method, the two ListSelectionListeners get triggered and they get row and column value -1, which is an exception when I use them to index. Is there anyway to prevent the fireTableDataChanged() method to trigger the two listener? Or is there any other method that will update the table data without triggering all the listeners registered to the model?

Here is the code:

 public class MonthTableModel extends AbstractTableModel {

/**
 * 
 */
private static final long serialVersionUID = 1L;
//private int currentYear, currentMonth;
private String[] columnName = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

@Override
public int getColumnCount() {
    // TODO Auto-generated method stub
    return 7;
}

@Override
public int getRowCount() {
    // TODO Auto-generated method stub
    return 6;
}

    public void updateData(int y, int m)
{
    /**
            do something to change the data of the table
            */
    fireTableDataChanged();
}
public String getColumnName(int column)
{

    return columnName[column];
}
@Override

public Object getValueAt(int row, int column) {
    // TODO Auto-generated method stub
    if ((row >= 0 && row < 6) && (column < 7 && column >= 0))
        return "CalendarDate";
    return "";
}


public boolean isCellEditable(int row, int col) 
   {
      return false;
   }

TabelMonthModel jTableMonthTable = new TableMonthModel();

SelectionListener listener = new SelectionListener(jTableMonthTable);

jTableMonthTable.getSelectionModel().addListSelectionListener(listener);
      jTableMonthTable.getColumnModel().getSelectionModel().addListSelectionListener(listener);


class SelectionListener implements ListSelectionListener  {
        JTable jtable;
        public RowSelectionListener(JTable j)
        {
            jtable = j;
        }
        @Override
        public void valueChanged(ListSelectionEvent e) {
            // TODO Auto-generated method stub
                   if (!e.getValueIsAdjusting())
                   { 
            int rowIndex = jtable.getSelectedRow();
            int colIndex = jtable.getSelectedColumn();
            System.out.println("row: " + rowIndex + "\n");
                        System.out.println("col: " + colIndex + "\n");
                   }
        }


    }

Thanks for your help

Was it helpful?

Solution

the two ListSelectionListeners get triggered and they get row and column value -1, which is an exception when I use them to index

I would guess the event gets generated because you remove all the data so the selection needs to be set to -1 indicating that no cells are selected anymore.

Your code should check for -1 and don't do the processing.

The other option is to remove the listener before you invoke the updateData() method and then add the listener after the model has been updated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top