The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing.table  [62 examples] > Events  [4 examples]

e966. Listening for Column-Related Changes in a JTable Component

Events are fired whenever a column is added, removed, resized, or moved.
    
table.getColumnModel().addColumnModelListener(
        new MyTableColumnModelListener(table));
    
    public class MyTableColumnModelListener implements TableColumnModelListener {
        JTable table;
    
        // It is necessary to keep the table since it is not possible
        // to determine the table from the event's source
        public MyTableColumnModelListener(JTable table) {
            this.table = table;
        }
    
        public void columnAdded(TableColumnModelEvent e) {
            int fromIndex = e.getFromIndex();
            int toIndex = e.getToIndex();
            // fromIndex and toIndex identify the range of added columns
        }
    
        public void columnRemoved(TableColumnModelEvent e) {
            int fromIndex = e.getFromIndex();
            int toIndex = e.getToIndex();
            // fromIndex and toIndex identify the range of removed columns
        }
    
        public void columnMoved(TableColumnModelEvent e) {
            int fromIndex = e.getFromIndex();
            int toIndex = e.getToIndex();
            // fromIndex and toIndex identify the range of columns being moved.
            // In the case of a user dragging a column, this event is fired as
            // the column is being dragged to its new position. Also, if the
            // column displaces another during dragging, the fromIndex and
            // toIndex show its new position; this new position is only
            // temporary until the user stops dragging the column.
        }
    
        public void columnMarginChanged(ChangeEvent e) {
            // The width of some column has changed.
            // The event does not identify which column.
        }
    
        public void columnSelectionChanged(ListSelectionEvent e) {
            // See e964 Listening for Selection Events in a JTable Component
        }
    }

 Related Examples
e964. Listening for Selection Events in a JTable Component
e965. Listening for Changes to the Rows and Columns of a JTable Component
e967. Listening for Clicks on a Column Header in a JTable Component

See also: Cells    Column Heads    Columns    Editing    Layout    Rows    Scrolling    Selection    Sorting    Table Model    Tool Tips   


© 2002 Addison-Wesley.