下面列出了javax.swing.table.TableColumnModel#getColumnCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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]);
}
}
}
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);
}
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);
}
}
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);
}
}
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]);
}
}
}
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);
}
}
}
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);
}
}
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();
}
}
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;
}
}
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);
}
}
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;
}
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);
}
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);
}
}
public void restoreColumns() {
TableColumnModel tcm = table.getColumnModel();
for (int i = 0; i < tcm.getColumnCount(); i++) {
restoreColumn(i);
}
}
/**
* (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;
}
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);
}
}
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;
}
@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();
}
/**
* 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;
}
/**
* 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;
}