Java Reference
In-Depth Information
To see a demonstration of the TableColumnModelListener , you can attach a listener to one
of your TableColumnModel objects:
TableColumnModel columnModel = table.getColumnModel();
columnModel.addColumnModelListener(...);
One such listener is shown in Listing 18-9. It doesn't do much besides print a message.
Nevertheless, you can use it to see when different events happen.
Listing 18-9. Table Column Model Listener
TableColumnModelListener tableColumnModelListener =
new TableColumnModelListener() {
public void columnAdded(TableColumnModelEvent e) {
System.out.println("Added");
}
public void columnMarginChanged(ChangeEvent e) {
System.out.println("Margin");
}
public void columnMoved(TableColumnModelEvent e) {
System.out.println("Moved");
}
public void columnRemoved(TableColumnModelEvent e) {
System.out.println("Removed");
}
public void columnSelectionChanged(ListSelectionEvent e) {
System.out.println("Selected");
}
};
Of course, you do need to create some code to elicit certain events. For instance, margins don't
appear out of thin air. But you can add the same column multiple times to add more columns (or
remove them). The program shown in Listing 18-10 tests the new TableColumnModelListener .
Listing 18-10. Playing with Table Columns
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.*;
import java.awt.*;
public class ColumnModelSample {
public static void main(String args[]) {
final Object rows[][] = {
{"one", "ichi - \u4E00"},
{"two", "ni - \u4E8C"},
{"three", "san - \u4E09"},
{"four", "shi - \u56DB"},
{"five", "go - \u4E94"},
Search WWH ::




Custom Search