VetoableTableColumnModelListener

Top  Previous  Next

 

In order to control which columns are added, removed or moved, you should write your own VetoableTableColumnModelListener.

VetoableTableColumnModelListener is a listener that is notified before table columns are added, moved or removed. The methods that are called are:

 

public void columnWillBeAdded(TableColumnModelEvent e) throws ColumnModelVetoException

public void columnWillBeMoved(TableColumnModelEvent e) throws ColumnModelVetoException

public void columnWillBeRemoved(TableColumnModelEvent e) throws ColumnModelVetoException

 

A VetoableTableColumnModelListener can 'veto' the event by inspecting the supplied TableColumnModelEvent and fire a ColumnModelVetoException where appropriate. If the event is allowed to occur, a ColumnModelVetoException should not be thrown.

 

From Sun's documentation regarding TableColumnModelEvents, having e as the TableColumnModelEvent, bear in mind that:

for removal events: e.getFromIndex() identifies the column to be removed

for addition events: e.getToIndex() identifies the column to be added

for move events: e.getFromIndex() identifies the column to move from while e.getToIndex() the column to move to.

 

VetoableTableColumnModel has methods for adding and removing VetoableTableColumnModelListeners:

 

public void addVetoableColumnModelListener(VetoableTableColumnModelListener l)

public void removeVetoableColumnModelListener(VetoableTableColumnModelListener l)

 

Example 1: Veto the removal of the third table column.

 

public class MyVetoableTableColumnModelListener implements VetoableTableColumnModelListener {

       public void columnWillBeAdded(TableColumnModelEvent e) throws ColumnModelVetoException {

       }

       public void columnWillBeMoved(TableColumnModelEvent e) throws ColumnModelVetoException {

       }

       public void columnWillBeRemoved(TableColumnModelEvent e) throws ColumnModelVetoException {

               if (e.getFromIndex() == 2) throw new ColumnModelVetoException(e);

       }

}

 

Example 2: Veto the move of a column before the third column.

 

public class MyVetoableTableColumnModelListener implements VetoableTableColumnModelListener {

       public void columnWillBeAdded(TableColumnModelEvent e) throws ColumnModelVetoException {

       }

       public void columnWillBeMoved(TableColumnModelEvent e) throws ColumnModelVetoException {

               if (e.getFromIndex() > 2 && e.getToIndex() <= 2) throw new ColumnModelVetoException(e);

       }

       public void columnWillBeRemoved(TableColumnModelEvent e) throws ColumnModelVetoException {

       }

}