Java Code Examples for javax.swing.table.TableColumn#getModelIndex()

The following examples show how to use javax.swing.table.TableColumn#getModelIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CopyDataToClipboard.java    From constellation with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: ProfilerColumnModel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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());
        }
    }
}
 
Example 3
Source File: BaseTableView.java    From consulo with Apache License 2.0 6 votes vote down vote up
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;
  }
}
 
Example 4
Source File: TableEditor.java    From pentaho-reporting with GNU Lesser General Public License v2.1 6 votes vote down vote up
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 );

}
 
Example 5
Source File: ProfilerColumnModel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example 6
Source File: PropertiesTableModel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** 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));
}
 
Example 7
Source File: SorterTableColumnModel.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public int getColumnIndexAtX(int XPosition) {

    for (TableColumn tc : columnList) {
        XPosition -= tc.getWidth();
        if (XPosition < 0) {
            return tc.getModelIndex();
        }
    }
    return -1;
}
 
Example 8
Source File: ThreadsPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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));
    }
}
 
Example 9
Source File: MainView.java    From HiJson with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: MatrixPropertyTable.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
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);
		}
	}
}
 
Example 11
Source File: ProfilerColumnModel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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);
    }
}
 
Example 12
Source File: PeakListTableColumnModel.java    From mzmine2 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 13
Source File: ProfilerColumnModel.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example 14
Source File: ThreadsPanel.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
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));
    }
}
 
Example 15
Source File: ColumnFilterDialogModel.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 16
Source File: DataToolPropsTable.java    From osp with GNU General Public License v3.0 5 votes vote down vote up
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);
}
 
Example 17
Source File: PeakListTableColumnModel.java    From mzmine3 with GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 18
Source File: Preferences.java    From pdfxtk with Apache License 2.0 4 votes vote down vote up
public void propertyChange(PropertyChangeEvent e) {
  TableColumn tc = (TableColumn)e.getSource();
  widths[tc.getModelIndex()] = tc.getWidth();
}
 
Example 19
Source File: ProfilerTable.java    From visualvm with GNU General Public License v2.0 4 votes vote down vote up
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();
        }
    }
}
 
Example 20
Source File: TaskListTable.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@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 );
}