下面列出了javax.swing.table.TableColumn#getModelIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Gets the indices of columns based on the AttributeSegment for that
* column.
*
* @param attrsegs A List of AttributeSegment instances corresponding to
* columns.
*
* @return The indices that correspond to the columns with names provided.
*/
private ArrayList<Integer> getColumnIndices(final List<AttributeSegment> attrsegs) {
// Instead of nested loops to match attrsegs and columns,
// we'll make a map of attrseg to column index,
// then pick out the attrsegs we want.
final HashMap<String, Integer> attrseg2index = new HashMap<>();
final GraphTableModel gtModel = (GraphTableModel) table.getModel();
for (int i = 0; i < table.getColumnModel().getColumnCount(); i++) {
final TableColumn tc = table.getColumnModel().getColumn(i);
final int modelIndex = tc.getModelIndex();
final AttributeSegment attrseg = gtModel.getAttributeSegment(modelIndex);
attrseg2index.put(attrseg.toString(), i);
}
final ArrayList<Integer> result = new ArrayList<>();
attrsegs.stream().forEach(attrseg -> result.add(attrseg2index.get(attrseg.toString())));
return result;
}
public static void store(final Storage storage, final JTable table) {
final TableColumnModel model = table.getTableHeader().getColumnModel();
final int columnCount = model.getColumnCount();
final boolean[] storedColumns = new boolean[columnCount];
Arrays.fill(storedColumns, false);
for (int i = 0; i < columnCount; i++) {
final TableColumn column = model.getColumn(i);
storage.put(widthPropertyName(i), String.valueOf(column.getWidth()));
final int modelIndex = column.getModelIndex();
storage.put(orderPropertyName(i), String.valueOf(modelIndex));
if (storedColumns[modelIndex]) {
LOG.error("columnCount: " + columnCount + " current: " + i + " modelINdex: " + modelIndex);
}
storedColumns[modelIndex] = true;
}
}
public void addColumn( final TableColumn column ) {
stopEditing();
if ( column.getHeaderValue() == null ) {
final int modelColumn = column.getModelIndex();
final String columnName = getModel().getColumnName( modelColumn );
if ( modelColumn == 0 ) {
column.setResizable( false );
column.setHeaderValue( columnName );
column.setPreferredWidth( 30 );
column.setMaxWidth( 30 );
column.setMinWidth( 30 );
} else {
final Class columnType = getModel().getColumnClass( modelColumn );
column.setHeaderValue( new TypedHeaderInformation( columnType, columnName ) );
}
}
getColumnModel().addColumn( column );
}
/** Fires a TableModelEvent - change of one column */
public void fireTableColumnChanged(int index) {
int columnModelIndex = index;
// reset the header value as well
Object list[] = listenerList.getListenerList();
for (int i = 0; i < list.length; i++) {
if (list[i] instanceof JTable) {
JTable jt = (JTable)list[i];
try {
TableColumn column = jt.getColumnModel().getColumn(index);
columnModelIndex = column.getModelIndex();
column.setHeaderValue(jt.getModel().getColumnName(columnModelIndex));
} catch (ArrayIndexOutOfBoundsException abe) {
// only catch exception
}
jt.getTableHeader().repaint();
}
}
fireTableChanged(new TableModelEvent(this, 0, getRowCount() - 1, columnModelIndex));
}
void showColumn(TableColumn column, ProfilerTable table) {
column.setMaxWidth(Integer.MAX_VALUE);
Integer width = hiddenColumnWidths.remove(column.getModelIndex());
column.setWidth(width != null ? width.intValue() :
getDefaultColumnWidth(column.getModelIndex()));
column.setMinWidth(minColumnWidth);
int toResizeIndex = getFitWidthColumn();
if (column.getModelIndex() == toResizeIndex) {
Enumeration<TableColumn> columns = getColumns();
while (columns.hasMoreElements()) {
TableColumn col = columns.nextElement();
int index = col.getModelIndex();
if (col.getModelIndex() != toResizeIndex && isColumnVisible(col))
col.setWidth(getDefaultColumnWidth(index));
}
table.doLayout();
}
}
void hideColumn(TableColumn column, ProfilerTable table) {
hiddenColumnWidths.put(column.getModelIndex(), column.getWidth());
column.setMinWidth(0);
column.setMaxWidth(0);
int selected = table.getSelectedColumn();
if (selected != -1 && getColumn(selected).equals(column)) {
int newSelected = getPreviousVisibleColumn(selected);
getSelectionModel().setSelectionInterval(newSelected, newSelected);
}
if (table.isSortable()) {
ProfilerRowSorter sorter = table._getRowSorter();
int sortColumn = sorter.getSortColumn();
if (sortColumn == column.getModelIndex()) {
int newSortColumn = table.convertColumnIndexToView(sortColumn);
newSortColumn = getPreviousVisibleColumn(newSortColumn);
sorter.setSortColumn(getColumn(newSortColumn).getModelIndex());
}
}
}
@Override
public int getColumnIndexAtX(int XPosition) {
for (TableColumn tc : columnList) {
XPosition -= tc.getWidth();
if (XPosition < 0) {
return tc.getModelIndex();
}
}
return -1;
}
public TableColumn getColumnByModelIndex(int modelIndex) {
Enumeration<TableColumn> allColumns = this.getColumns();
while (allColumns.hasMoreElements()) {
TableColumn col = allColumns.nextElement();
if (col.getModelIndex() == modelIndex)
return col;
}
return null;
}
public void addColumn(TableColumn aColumn) {
int modelColumn = aColumn.getModelIndex();
if(aColumn.getHeaderValue()==null) {
String columnName = getModel().getColumnName(modelColumn);
aColumn.setHeaderValue(columnName);
}
// workaround to prevent adding multiple columns with same model index
for (int i = 0; i < getColumnModel().getColumnCount(); i++) {
int index = getColumnModel().getColumn(i).getModelIndex();
if (index==modelColumn) return;
}
getColumnModel().addColumn(aColumn);
}
@Override
public void columnAdded(TableColumnModelEvent e) {
int viewIndex = e.getToIndex();
TableColumn column = columnModel.getColumn(viewIndex);
int modelIndex = column.getModelIndex();
Class<?> columnClass = tableModel.getColumnClass(modelIndex);
ColumnFilterData<?> columnFilterData =
createColumnFilterData(tableModel, modelIndex, viewIndex, columnClass);
if (columnFilterData.isFilterable()) {
allFilters.add(columnFilterData);
}
updateColumnViewIndices();
notifyFilterChanged();
}
private void repaintTimeline() {
JTableHeader header = threadsTable.getTableHeader();
TableColumn draggedColumn = header.getDraggedColumn();
if (draggedColumn != null && draggedColumn.getModelIndex() == 2) {
header.repaint();
} else {
int _column = threadsTable.convertColumnIndexToView(2);
header.repaint(header.getHeaderRect(_column));
}
}
void setDefaultColumnWidth(int width) {
defaultColumnWidth = width;
Enumeration<TableColumn> columns = getColumns();
while (columns.hasMoreElements()) {
TableColumn column = columns.nextElement();
int index = column.getModelIndex();
if (defaultColumnWidths == null || defaultColumnWidths.get(index) == null)
if (index != fitWidthColumn) column.setWidth(width);
}
}
public TableColumn getColumnByModelIndex(int modelIndex) {
Enumeration<TableColumn> allColumns = this.getColumns();
while (allColumns.hasMoreElements()) {
TableColumn col = allColumns.nextElement();
if (col.getModelIndex() == modelIndex)
return col;
}
return null;
}
void setDefaultColumnWidth(int width) {
defaultColumnWidth = width;
Enumeration<TableColumn> columns = getColumns();
while (columns.hasMoreElements()) {
TableColumn column = columns.nextElement();
int index = column.getModelIndex();
if (defaultColumnWidths == null || defaultColumnWidths.get(index) == null)
if (index != fitWidthColumn) column.setWidth(width);
}
}
public void removeColumn(int index) {
TableColumn column = getColumnModel().getColumn(index);
int modelIndex = column.getModelIndex();
Vector<?> modelData = model.getDataVector();
Vector<?> columnIdentifiers = model.getColumnIdentifiers();
// remove the column from the table
removeColumn(column);
// remove the column header from the table model
columnIdentifiers.removeElementAt(modelIndex);
// remove the column data
for (Object row : modelData) {
((Vector<?>) row).removeElementAt(modelIndex);
}
model.setDataVector(modelData, columnIdentifiers);
// correct the model indices in the TableColumn objects
Enumeration<TableColumn> columns = getColumnModel().getColumns();
while (columns.hasMoreElements()) {
TableColumn currentColumn = columns.nextElement();
if (currentColumn.getModelIndex() >= modelIndex) {
currentColumn.setModelIndex(currentColumn.getModelIndex() - 1);
}
}
}
private int widestCellInColumn(JTable table,TableColumn col) {
int c = col.getModelIndex();
int width = 0, maxw = 0;
for (int r = 0; r < table.getRowCount(); r++) {
TableCellRenderer renderer = table.getCellRenderer(r, c);
Component comp = renderer.getTableCellRendererComponent(table, table.getValueAt(r, c), false, false, r, c);
width = comp.getPreferredSize().width;
maxw = width > maxw ? width : maxw;
}
if(maxw<90)maxw = 90;
return maxw + 10;
}
private void repaintTimeline() {
JTableHeader header = threadsTable.getTableHeader();
TableColumn draggedColumn = header.getDraggedColumn();
if (draggedColumn != null && draggedColumn.getModelIndex() == 2) {
header.repaint();
} else {
int _column = threadsTable.convertColumnIndexToView(2);
header.repaint(header.getHeaderRect(_column));
}
}
@Override
public void setDraggedColumn( TableColumn aColumn ) {
if( null != aColumn && aColumn.getModelIndex() == 0 )
return; //don't allow the first column to be dragged
super.setDraggedColumn( aColumn );
}
public void doLayout() {
ProfilerColumnModel cModel = _getColumnModel();
JTableHeader header = getTableHeader();
TableColumn res = header == null ? null : header.getResizingColumn();
if (res != null) {
// Resizing column
int delta = getWidth() - cModel.getTotalColumnWidth();
TableColumn next = cModel.getNextVisibleColumn(res);
if (res == next) {
res.setWidth(res.getWidth() + delta);
} else {
next.setWidth(next.getWidth() + delta);
}
} else {
// Resizing table
int toResizeIndex = cModel.getFitWidthColumn();
if (toResizeIndex == -1) {
super.doLayout();
} else {
Enumeration<TableColumn> columns = cModel.getColumns();
TableColumn toResizeColumn = null;
int columnsWidth = 0;
while (columns.hasMoreElements()) {
TableColumn column = columns.nextElement();
if (column.getModelIndex() == toResizeIndex) {
if (!cModel.isColumnVisible(column)) {
super.doLayout();
return;
}
toResizeColumn = column;
} else {
columnsWidth += column.getWidth();
}
}
if (toResizeColumn != null) toResizeColumn.setWidth(getWidth() - columnsWidth);
// instead of super.doLayout()
layout();
}
}
}
public void propertyChange(PropertyChangeEvent e) {
TableColumn tc = (TableColumn)e.getSource();
widths[tc.getModelIndex()] = tc.getWidth();
}