Java Code Examples for javax.swing.table.TableColumnModel#getColumnCount()

The following examples show how to use javax.swing.table.TableColumnModel#getColumnCount() . 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: SampledResultsPanel.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
private void setColumnsData() {
    barRenderer = getBarCellRenderer();

    TableColumnModel colModel = resTable.getColumnModel();
    colModel.getColumn(0).setPreferredWidth(minNamesColumnWidth);

    int index;

    for (int i = 0; i < colModel.getColumnCount(); i++) {
        index = resTableModel.getRealColumn(i);

        if (index == 0) {
            colModel.getColumn(i).setPreferredWidth(minNamesColumnWidth);
        } else {
            colModel.getColumn(i).setPreferredWidth(columnWidths[index - 1]);
        }

        if (index == 1) {
            colModel.getColumn(i).setCellRenderer(barRenderer);
        } else {
            colModel.getColumn(i).setCellRenderer(columnRenderers[index]);
        }
    }
}
 
Example 2
Source File: JTreeTable.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void updateTreeTableHeader() {
    TableColumnModel tableColumnModel = getColumnModel();
    int n = tableColumnModel.getColumnCount();

    for (int i = 0; i < n; i++) {
        tableColumnModel.getColumn(i).setHeaderRenderer(headerRenderer);
    }

    if (tableHeader != getTableHeader()) {
        if (tableHeader != null) {
            tableHeader.removeMouseListener(headerListener);
        }

        if (tableHeader != null) {
            tableHeader.removeMouseMotionListener(headerListener);
        }

        tableHeader = getTableHeader();
        tableHeader.addMouseListener(headerListener);
        tableHeader.addMouseMotionListener(headerListener);
        updateTreeTable();
    }
}
 
Example 3
Source File: TaskTextViewerPanel.java    From moa with GNU General Public License v3.0 6 votes vote down vote up
private void rescaleTableColumns() {
    // iterate over all columns to resize them individually
    TableColumnModel columnModel = previewTable.getColumnModel();
    for (int columnIdx = 0; columnIdx < columnModel.getColumnCount(); ++columnIdx) {
        // get the current column
        TableColumn column = columnModel.getColumn(columnIdx);
        // get the renderer for the column header to calculate the preferred
        // with for the header
        TableCellRenderer renderer = column.getHeaderRenderer();
        // check if the renderer is null
        if (renderer == null) {
            // if it is null use the default renderer for header
            renderer = previewTable.getTableHeader().getDefaultRenderer();
        }
        // create a cell to calculate its preferred size
        Component comp = renderer.getTableCellRendererComponent(previewTable, column.getHeaderValue(), false, false,
                0, columnIdx);
        int width = comp.getPreferredSize().width;
        // set the maximum width which was calculated
        column.setPreferredWidth(width);
    }
}
 
Example 4
Source File: PropertiesTable.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void setDefaultColumnSize() {
    int width = table.getWidth();
    TableColumnModel columnModel = table.getColumnModel();
    if (columns == null || columnModel == null)
        return;
    if (columnModel.getColumnCount() != columns.length)
        return;
    for (int i = 0; i < columns.length; i++) {
        String col = columns[i];                                
        sorter.setColumnComparator(i, null);                    
        if (col.equals(PropertiesTableModel.COLUMN_NAME_NAME)) {
            columnModel.getColumn(i).setPreferredWidth(width * 20 / 100);
        } else if (col.equals(PropertiesTableModel.COLUMN_NAME_VALUE)) {
            columnModel.getColumn(i).setPreferredWidth(width * 40 / 100);
        }
    }
}
 
Example 5
Source File: LivenessResultsPanel.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void setColumnsData() {
    barRenderer = getBarCellRenderer();

    TableColumnModel colModel = resTable.getColumnModel();
    colModel.getColumn(0).setPreferredWidth(minNamesColumnWidth);

    int index;

    for (int i = 0; i < colModel.getColumnCount(); i++) {
        index = resTableModel.getRealColumn(i);

        if (index == 0) {
            colModel.getColumn(i).setPreferredWidth(minNamesColumnWidth);
        } else {
            colModel.getColumn(i).setPreferredWidth(columnWidths[index - 1]);
        }

        if (index == 1) {
            colModel.getColumn(i).setCellRenderer(barRenderer);
        } else {
            colModel.getColumn(i).setCellRenderer(columnRenderers[index]);
        }
    }
}
 
Example 6
Source File: DetailsPanel.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
private void paintVerticalLines(Graphics g) {
    int height = getHeight();
    int viewHeight = view.getHeight();
    if (viewHeight >= height) return;

    g.setColor(background);
    g.fillRect(0, viewHeight, getWidth(), getHeight() - viewHeight);

    int cellX = 0;
    int cellWidth;
    TableColumnModel model = view.getColumnModel();
    int columnCount = model.getColumnCount();
    
    g.setColor(DetailsTable.DEFAULT_GRID_COLOR);
    for (int i = 0; i < columnCount; i++) {
        cellWidth = model.getColumn(i).getWidth();
        cellX += cellWidth;
        g.drawLine(cellX - 1, viewHeight, cellX - 1, height);
    }
}
 
Example 7
Source File: DetailsPanel.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
private void paintVerticalLines(Graphics g) {
    int height = getHeight();
    int viewHeight = view.getHeight();
    if (viewHeight >= height) return;

    g.setColor(background);
    g.fillRect(0, viewHeight, getWidth(), getHeight() - viewHeight);

    int cellX = 0;
    int cellWidth;
    TableColumnModel model = view.getColumnModel();
    int columnCount = model.getColumnCount();
    
    g.setColor(DetailsTable.DEFAULT_GRID_COLOR);
    for (int i = 0; i < columnCount; i++) {
        cellWidth = model.getColumn(i).getWidth();
        cellX += cellWidth;
        g.drawLine(cellX - 1, viewHeight, cellX - 1, height);
    }
}
 
Example 8
Source File: Preferences.java    From pdfxtk with Apache License 2.0 6 votes vote down vote up
void grab(TableColumnModel tcm) {
     order  = new int[tcm.getColumnCount()];
     
     int maxidx = -1;
     for(int i = 0; i < order.length; i++) {
int idx = tcm.getColumn(i).getModelIndex();
order[i] = idx;
if(idx > maxidx)
  maxidx = idx;
     }
     
     widths = new int[maxidx + 1];
     
     for(int i = 0; i < order.length; i++)
widths[order[i]] = tcm.getColumn(i).getWidth();
     
     installListeners(tcm);
   }
 
Example 9
Source File: JXTableDecorator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void drawVerticalLines(Graphics g, final int rowCount, final int height) {

        g.setColor(ResultSetJXTable.GRID_COLOR);
        TableColumnModel colModel = getColumnModel();
        int x = 0;
        for (int i = 0; i < colModel.getColumnCount(); ++i) {
            TableColumn column = colModel.getColumn(i);
            x += column.getWidth();
            g.drawLine(x - 1, rowCount * rowHeight, x - 1, height);
        }
    }
 
Example 10
Source File: GameStatisticsGUI.java    From Carcassonne with Eclipse Public License 2.0 5 votes vote down vote up
private void buildTable(Round round) {
    table = new JTable(new GameStatisticsModel(round));
    // Columns:
    TableColumnModel model = table.getColumnModel();
    CellRenderer renderer = new CellRenderer(round);
    for (int i = 0; i < model.getColumnCount(); i++) {
        model.getColumn(i).setCellRenderer(renderer);
    }
    // Header:
    JTableHeader header = table.getTableHeader();
    header.setDefaultRenderer(new HeaderRenderer());
    header.setReorderingAllowed(false);
    table.setBackground(BODY_COLOR);
}
 
Example 11
Source File: TableColumnAdjuster.java    From keystore-explorer with GNU General Public License v3.0 5 votes vote down vote up
public void restoreColumns() {
	TableColumnModel tcm = table.getColumnModel();

	for (int i = 0; i < tcm.getColumnCount(); i++) {
		restoreColumn(i);
	}
}
 
Example 12
Source File: TaskListTableUI.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * (copy & paste from BasicTableUI)
 */
private int viewIndexForColumn(TableColumn aColumn) {
    TableColumnModel cm = table.getColumnModel();
    for (int column = 0; column < cm.getColumnCount(); column++) {
        if (cm.getColumn(column) == aColumn) {
            return column;
        }
    }
    return -1;
}
 
Example 13
Source File: ExampleSourceConfigurationWizardValueTypeTable.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
public void update() {
	((AbstractTableModel) getModel()).fireTableStructureChanged();
	TableColumnModel columnModel = getColumnModel();
	for (int i = 0; i < columnModel.getColumnCount(); i++) {
		TableColumn tableColumn = columnModel.getColumn(i);
		tableColumn.setPreferredWidth(120);
	}
}
 
Example 14
Source File: SynthTableUI.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private int viewIndexForColumn(TableColumn aColumn) {
    TableColumnModel cm = table.getColumnModel();
    for (int column = 0; column < cm.getColumnCount(); column++) {
        if (cm.getColumn(column) == aColumn) {
            return column;
        }
    }
    return -1;
}
 
Example 15
Source File: DualView.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void addWidthListenersTo(JTable treeView) {
  TableColumnModel columnModel = treeView.getColumnModel();
  int columnCount = columnModel.getColumnCount();
  for (int i = 0; i < columnCount; i++) {
    columnModel.getColumn(i).addPropertyChangeListener(myPropertyChangeListener);
  }
}
 
Example 16
Source File: SynthTableUI.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private int viewIndexForColumn(TableColumn aColumn) {
    TableColumnModel cm = table.getColumnModel();
    for (int column = 0; column < cm.getColumnCount(); column++) {
        if (cm.getColumn(column) == aColumn) {
            return column;
        }
    }
    return -1;
}
 
Example 17
Source File: OutlineView.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int getColumnXPos(OutlineViewOutline outline, int column) {
    if (column < 0) {
        if (!outline.getComponentOrientation().isLeftToRight()) {
            return outline.getWidth();
        } else {
            return 0;
        }
    } else if (column >= outline.getColumnCount()) {
        if (outline.getComponentOrientation().isLeftToRight()) {
            return outline.getWidth();
        } else {
            return 0;
        }
    } else {
        TableColumnModel cm = outline.getColumnModel();
        int x = 0;
        if (outline.getComponentOrientation().isLeftToRight()) {
            for (int i = 0; i < column; i++) {
                x += cm.getColumn(i).getWidth();
            }
        } else {
            for(int i = cm.getColumnCount()-1; i > column; i--) {
                x += cm.getColumn(i).getWidth();
            }
        }
        return x;
    }
}
 
Example 18
Source File: SubstanceTableHeaderUI.java    From radiance with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void paint(Graphics g, JComponent c) {
    if (header.getColumnModel().getColumnCount() <= 0) {
        return;
    }
    boolean ltr = header.getComponentOrientation().isLeftToRight();

    Rectangle clip = g.getClipBounds();
    Point left = clip.getLocation();
    Point right = new Point(clip.x + clip.width - 1, clip.y);

    TableColumnModel cm = header.getColumnModel();
    int[] selectedColumns = cm.getSelectedColumns();
    Set<Integer> selected = new HashSet<>();
    for (int sel : selectedColumns)
        selected.add(sel);

    int cMin = header.columnAtPoint(ltr ? left : right);
    int cMax = header.columnAtPoint(ltr ? right : left);
    // This should never happen.
    if (cMin == -1) {
        cMin = 0;
    }
    // If the table does not have enough columns to fill the view we'll get
    // -1.
    // Replace this with the index of the last column.
    if (cMax == -1) {
        cMax = cm.getColumnCount() - 1;
    }

    TableColumn draggedColumn = header.getDraggedColumn();
    int columnWidth;
    Rectangle cellRect = header.getHeaderRect(ltr ? cMin : cMax);
    TableColumn aColumn;
    if (ltr) {
        for (int column = cMin; column <= cMax; column++) {
            aColumn = cm.getColumn(column);
            columnWidth = aColumn.getWidth();
            cellRect.width = columnWidth;
            if (aColumn != draggedColumn) {
                this.paintCell(g, cellRect, column, selected.contains(column));
            }
            cellRect.x += columnWidth;
        }
    } else {
        for (int column = cMax; column >= cMin; column--) {
            aColumn = cm.getColumn(column);
            columnWidth = aColumn.getWidth();
            cellRect.width = columnWidth;
            if (aColumn != draggedColumn) {
                this.paintCell(g, cellRect, column, selected.contains(column));
            }
            cellRect.x += columnWidth;
        }
    }

    this.paintGrid(g, c);

    // Paint the dragged column if we are dragging.
    if (draggedColumn != null) {
        int draggedColumnIndex = viewIndexForColumn(draggedColumn);
        Rectangle draggedCellRect = header.getHeaderRect(draggedColumnIndex);

        // Draw a gray well in place of the moving column.
        g.setColor(header.getParent().getBackground());
        g.fillRect(draggedCellRect.x, draggedCellRect.y, draggedCellRect.width,
                draggedCellRect.height);

        draggedCellRect.x += header.getDraggedDistance();

        // Fill the background.
        g.setColor(header.getBackground());
        g.fillRect(draggedCellRect.x, draggedCellRect.y, draggedCellRect.width,
                draggedCellRect.height);

        // Fix for https://github.com/kirill-grouchnikov/substance/issues/70 -
        // don't paint the dragged cell if dragged column index is negative (otherwise
        // it will crash in ColumnModel.getColumn)
        if (draggedColumnIndex >= 0) {
            this.paintCell(g, draggedCellRect, draggedColumnIndex,
                    selected.contains(draggedColumnIndex));
        }
    }

    // Remove all components in the rendererPane.
    rendererPane.removeAll();
}
 
Example 19
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 3 votes vote down vote up
/**
 * Maps the index of the column in the {@code cm} at
 * {@code modelColumnIndex} to the index of the column
 * in the view.  Returns the index of the
 * corresponding column in the view; returns {@code -1} if this column
 * is not being displayed. If {@code modelColumnIndex} is less than zero,
 * returns {@code modelColumnIndex}.
 *
 * @param cm the table model
 * @param modelColumnIndex the index of the column in the model
 * @return the index of the corresponding column in the view
 *
 * @see JTable#convertColumnIndexToView(int)
 * @see javax.swing.plaf.basic.BasicTableHeaderUI
 */
public static int convertColumnIndexToView(TableColumnModel cm,
                                    int modelColumnIndex) {
    if (modelColumnIndex < 0) {
        return modelColumnIndex;
    }
    for (int column = 0; column < cm.getColumnCount(); column++) {
        if (cm.getColumn(column).getModelIndex() == modelColumnIndex) {
            return column;
        }
    }
    return -1;
}
 
Example 20
Source File: SwingUtilities2.java    From jdk8u-jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Maps the index of the column in the {@code cm} at
 * {@code modelColumnIndex} to the index of the column
 * in the view.  Returns the index of the
 * corresponding column in the view; returns {@code -1} if this column
 * is not being displayed. If {@code modelColumnIndex} is less than zero,
 * returns {@code modelColumnIndex}.
 *
 * @param cm the table model
 * @param modelColumnIndex the index of the column in the model
 * @return the index of the corresponding column in the view
 *
 * @see JTable#convertColumnIndexToView(int)
 * @see javax.swing.plaf.basic.BasicTableHeaderUI
 */
public static int convertColumnIndexToView(TableColumnModel cm,
                                    int modelColumnIndex) {
    if (modelColumnIndex < 0) {
        return modelColumnIndex;
    }
    for (int column = 0; column < cm.getColumnCount(); column++) {
        if (cm.getColumn(column).getModelIndex() == modelColumnIndex) {
            return column;
        }
    }
    return -1;
}