日期:2014-05-20 浏览次数:20822 次
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class LineNumber {
public static void decorate(JTable table) {
if( table == null )
throw new NullPointerException("table is null.");
TableModel model = table.getModel();
if( !(model instanceof LineNumberTableModel) )
table.setModel(new LineNumberTableModel(model));
}
private static class LineNumberTableModel implements TableModel {
private final TableModel model;
private LineNumberTableModel(TableModel model) {
this.model = model;
}
@Override
public int getRowCount() {
return model.getRowCount();
}
@Override
public int getColumnCount() {
return 1 + model.getColumnCount();
}
@Override
public String getColumnName(int columnIndex) {
return columnIndex == 0 ? "Row" : model.getColumnName(columnIndex - 1);
}