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 Listening for Selection Events in a JTable Component } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.