下面列出了javax.swing.table.TableColumnModel#getColumn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
private void updateTableColumnSizes() {
ETable table = tableView.getTable();
Font font = tableView.getFont();
FontMetrics fm = tableView.getFontMetrics(font);
int maxCharWidth = fm.charWidth('A');
int editingBorder = 4;
TableColumnModel columnModel = table.getColumnModel();
TableColumn nameColumn = columnModel.getColumn(0);
nameColumn.setPreferredWidth(8 * maxCharWidth + editingBorder); // 8 chars for name
TableColumn keyColumn = columnModel.getColumn(1);
// Single char for key (but 3 chars to prevent "..." in column header)
keyColumn.setPreferredWidth(3 * maxCharWidth + editingBorder);
keyColumn.setMinWidth(keyColumn.getPreferredWidth());
TableColumn locationColumn = columnModel.getColumn(2);
Insets insets = tableView.getBorder().getBorderInsets(tableView);
int remainingWidth = tableView.getParent().getWidth() - insets.left - insets.right;
remainingWidth -= 2 * columnModel.getColumnMargin();
remainingWidth -= nameColumn.getPreferredWidth();
remainingWidth -= keyColumn.getPreferredWidth();
locationColumn.setPreferredWidth(remainingWidth); // remaining space for location
}
@Override public void mouseClicked(MouseEvent e) {
JTableHeader header = (JTableHeader) e.getComponent();
JTable tbl = header.getTable();
TableColumnModel columnModel = tbl.getColumnModel();
TableModel m = tbl.getModel();
int vci = columnModel.getColumnIndexAtX(e.getX());
int mci = tbl.convertColumnIndexToModel(vci);
if (mci == targetColumnIndex && m.getRowCount() > 0) {
TableColumn column = columnModel.getColumn(vci);
boolean b = column.getHeaderValue() == Status.DESELECTED;
for (int i = 0; i < m.getRowCount(); i++) {
m.setValueAt(b, i, mci);
}
column.setHeaderValue(b ? Status.SELECTED : Status.DESELECTED);
// header.repaint();
}
}
void setViewColumnsWidth(JTable jt) {
/*
* Resize column 0, 1
*/
TableColumnModel colmodel = jt.getColumnModel();
TableColumn col0 = colmodel.getColumn(0);
col0.setPreferredWidth(175);
TableColumn col1 = colmodel.getColumn(1);
col1.setPreferredWidth(175);
return;
}
private void setColumnEditors() {
TableColumnModel tcm = getColumnModel();
for (int i = 0; i < columnNames.length; i++) {
TableColumn tc = tcm.getColumn(i);
if (isColumnEditable(i)) {
tc.setCellEditor(valueCellEditor);
} else {
tc.setCellEditor(editor);
}
}
}
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
ElistTablePanel tmp = left;
left = top;
top = tmp;
model.close();
model = createModel();
if ((Boolean) getValue(SELECTED_KEY)) {
model.setRevert(true);
} else {
model.setRevert(false);
}
table.setModel(model);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableHeader = new TopTablePanel(top, table);
table.setTableHeader(tableHeader);
table.setRowHeight(CELL_BORDER);
tableHeader.revalidate();
tableHeader.repaint();
TableColumnModel columnModel = table.getColumnModel();
for (int i = 0; i < columnModel.getColumnCount(); i++) {
TableColumn column = columnModel.getColumn(i);
column.setMaxWidth(CELL_BORDER);
column.setMinWidth(CELL_BORDER);
}
pane.setRowHeaderView(left);
updateHeadders();
pane.revalidate();
pane.repaint();
pane.setViewportView(table);
}
private int getHeaderHeight() {
int height = 0;
TableColumnModel columnModel = header.getColumnModel();
for (int column = 0; column < columnModel.getColumnCount(); column++) {
TableColumn tc = columnModel.getColumn(column);
Component comp = getHeaderRenderer(column);
int rendererHeight = comp.getPreferredSize().height;
for (Object o: ((GroupableTableHeader) header).getColumnGroups(tc)) {
ColumnGroup cg = (ColumnGroup) o;
rendererHeight += cg.getSize(header).height;
}
height = Math.max(height, rendererHeight);
}
return height;
}
@Override public void paint(Graphics g, JComponent c) {
Rectangle clip = g.getClipBounds();
Point left = clip.getLocation();
Point right = new Point(clip.x + clip.width - 1, clip.y);
TableColumnModel cm = header.getColumnModel();
int colMin = header.columnAtPoint(left);
int colMax = header.columnAtPoint(right);
Rectangle cellRect = header.getHeaderRect(colMin);
int headerY = cellRect.y;
int headerHeight = cellRect.height;
Map<ColumnGroup, Rectangle> h = new ConcurrentHashMap<>();
// int columnMargin = header.getColumnModel().getColumnMargin();
// int columnWidth;
for (int column = colMin; column <= colMax; column++) {
TableColumn tc = cm.getColumn(column);
cellRect.y = headerY;
cellRect.setSize(tc.getWidth(), headerHeight);
int groupHeight = 0;
for (Object o: ((GroupableTableHeader) header).getColumnGroups(tc)) {
ColumnGroup cg = (ColumnGroup) o;
Rectangle groupRect = Optional.ofNullable(h.get(cg))
.orElseGet(() -> {
Rectangle r = createRect(cellRect.getLocation(), cg.getSize(header));
h.put(cg, r);
return r;
});
paintCellGroup(g, groupRect, cg);
groupHeight += groupRect.height;
cellRect.height = headerHeight - groupHeight;
cellRect.y = groupHeight;
}
paintCell(g, cellRect, column);
cellRect.x += cellRect.width;
}
}
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 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;
}
void installListeners(TableColumnModel tcm) {
for(int i = 0; i < tcm.getColumnCount(); i++) {
TableColumn col = tcm.getColumn(i);
col.removePropertyChangeListener(this);
col.addPropertyChangeListener(this);
}
}
private void setColumnEditors() {
TableColumnModel tcm = getColumnModel();
for (int i = 0; i < columnNames.length; i++) {
TableColumn tc = tcm.getColumn(i);
if (isColumnEditable(i)) {
tc.setCellEditor(valueCellEditor);
} else {
tc.setCellEditor(editor);
}
}
}
private JScrollPane createConditionsTable(final String labelName) {
conditionsTable = new JTable() {
private static final long serialVersionUID = 1L;
@Override
public Class getColumnClass(int column) {
if (column == 2) {
return Boolean.class;
} else {
return super.getColumnClass(column);
}
}
};
conditionsTable.setName(labelName);
conditionsTable.setRowSelectionAllowed(true);
bindingCtx.bind("conditions", new ConditionsTableAdapter(conditionsTable));
bindingCtx.bindEnabledState("conditions", false, "updateMode", true);
conditionsTable.addMouseListener(createExpressionEditorMouseListener(conditionsTable, true));
final JTableHeader tableHeader = conditionsTable.getTableHeader();
tableHeader.setName(labelName);
tableHeader.setReorderingAllowed(false);
tableHeader.setResizingAllowed(true);
final TableColumnModel columnModel = conditionsTable.getColumnModel();
columnModel.setColumnSelectionAllowed(false);
final TableColumn nameColumn = columnModel.getColumn(0);
nameColumn.setPreferredWidth(100);
nameColumn.setCellRenderer(new TCR());
final TableColumn expressionColumn = columnModel.getColumn(1);
expressionColumn.setPreferredWidth(360);
expressionColumn.setCellRenderer(new TCR());
final ExprEditor cellEditor = new ExprEditor(true);
expressionColumn.setCellEditor(cellEditor);
bindingCtx.addPropertyChangeListener("updateMode", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
final boolean enabled = Boolean.FALSE.equals(evt.getNewValue());
cellEditor.button.setEnabled(enabled);
}
});
final TableColumn outputColumn = columnModel.getColumn(2);
outputColumn.setPreferredWidth(40);
final JScrollPane pane = new JScrollPane(conditionsTable);
pane.setName(labelName);
pane.setPreferredSize(new Dimension(PREFERRED_TABLE_WIDTH, 80));
return pane;
}
private void saveWidths () {
if (columns == null) {
return;
}
int i, k = columns.length;
if (k == 0) {
return ;
}
TableColumnModel tcm = treeTable.getTable().getColumnModel();
ETableColumnModel ecm = (ETableColumnModel) tcm;
Enumeration<TableColumn> etc = tcm.getColumns();
boolean defaultState = true;
while(etc.hasMoreElements()) {
if (etc.nextElement().getWidth() != 75) {
defaultState = false;
break;
}
}
if (defaultState) {
// All columns have the default width 75.
// It's very likely that the table was not fully initialized => do not save anything.
return ;
}
logger.fine("\nsaveWidths():");
for (i = 0; i < k; i++) {
if (isHiddenColumn(i)) {
continue;
}
int visibleOrder = columnVisibleMap[i];
logger.log(Level.FINE, " visibleOrder[{0}] = {1}, ", new Object[]{i, visibleOrder});
if (visibleOrder >= tcm.getColumnCount()) {
continue;
}
TableColumn tc;
try {
tc = tcm.getColumn (visibleOrder);
} catch (ArrayIndexOutOfBoundsException aioobex) {
logger.log(Level.SEVERE,
"Column("+i+") "+columns[i].getName()+" visible index = "+visibleOrder+
", columnVisibleMap = "+java.util.Arrays.toString(columnVisibleMap)+
", num of columns = "+tcm.getColumnCount(),
aioobex);
continue ;
}
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, " GUI column = {0}", tc.getHeaderValue());
}
if (columns[i] instanceof Column) {
if (logger.isLoggable(Level.FINE)) {
logger.fine(" Setting width "+tc.getWidth()+" from "+tc.getHeaderValue()+" to "+columns[i].getDisplayName()+"["+i+"]");
}
((Column) columns[i]).setColumnWidth(tc.getWidth());
}
}
}
private void initComponents() {
JTable impl = new JTable(new DefaultTableModel(new Object[] { "" }, 0)); // NOI18N
TableColumnModel colMod = impl.getColumnModel();
final TableColumn col = colMod.getColumn(0);
impl.setFocusable(false);
header = new Header(colMod);
impl.setTableHeader(header);
header.setResizingAllowed(false);
header.setReorderingAllowed(false);
final TableCellRenderer renderer = header.getDefaultRenderer();
header.setDefaultRenderer(new TableCellRenderer() {
public Component getTableCellRendererComponent(
JTable table, Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
Component component = renderer.getTableCellRendererComponent(
table, getRendererValue(), isSelected(),
isSelected(), row, processMouseEvents() ? 0 : 1);
setupRenderer(component);
col.setWidth(header.getWidth());
return component;
}
});
JScrollPane scroll = new JScrollPane(impl, JScrollPane.VERTICAL_SCROLLBAR_NEVER,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER) {
public Dimension getPreferredSize() { return header.getPreferredSize(); }
public void reshape(int x, int y, int width, int height) {
header.setPreferredSize(new Dimension(width, height));
super.reshape(x, y, width, height);
}
};
scroll.setBorder(BorderFactory.createEmptyBorder());
scroll.setViewportBorder(BorderFactory.createEmptyBorder());
setLayout(new OverlayLayout(this));
add(scroll);
}
private void paintCells(SynthContext context, Graphics g, int rMin,
int rMax, int cMin, int cMax) {
JTableHeader header = table.getTableHeader();
TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn();
TableColumnModel cm = table.getColumnModel();
int columnMargin = cm.getColumnMargin();
Rectangle cellRect;
TableColumn aColumn;
int columnWidth;
if (table.getComponentOrientation().isLeftToRight()) {
for(int row = rMin; row <= rMax; row++) {
cellRect = table.getCellRect(row, cMin, false);
for(int column = cMin; column <= cMax; column++) {
aColumn = cm.getColumn(column);
columnWidth = aColumn.getWidth();
cellRect.width = columnWidth - columnMargin;
if (aColumn != draggedColumn) {
paintCell(context, g, cellRect, row, column);
}
cellRect.x += columnWidth;
}
}
} else {
for(int row = rMin; row <= rMax; row++) {
cellRect = table.getCellRect(row, cMin, false);
aColumn = cm.getColumn(cMin);
if (aColumn != draggedColumn) {
columnWidth = aColumn.getWidth();
cellRect.width = columnWidth - columnMargin;
paintCell(context, g, cellRect, row, cMin);
}
for(int column = cMin+1; column <= cMax; column++) {
aColumn = cm.getColumn(column);
columnWidth = aColumn.getWidth();
cellRect.width = columnWidth - columnMargin;
cellRect.x -= columnWidth;
if (aColumn != draggedColumn) {
paintCell(context, g, cellRect, row, column);
}
}
}
}
// Paint the dragged column if we are dragging.
if (draggedColumn != null) {
paintDraggedArea(context, g, rMin, rMax, draggedColumn, header.getDraggedDistance());
}
// Remove any renderers that may be left in the rendererPane.
rendererPane.removeAll();
}
private void paintDraggedColumnBorders( Graphics g, JComponent c ) {
TableColumn draggedColumn = header.getDraggedColumn();
if( draggedColumn == null )
return;
// find index of dragged column
TableColumnModel columnModel = header.getColumnModel();
int columnCount = columnModel.getColumnCount();
int draggedColumnIndex = -1;
for( int i = 0; i < columnCount; i++ ) {
if( columnModel.getColumn( i ) == draggedColumn ) {
draggedColumnIndex = i;
break;
}
}
if( draggedColumnIndex < 0 )
return;
float lineWidth = UIScale.scale( 1f );
float topLineIndent = lineWidth;
float bottomLineIndent = lineWidth * 3;
Rectangle r = header.getHeaderRect( draggedColumnIndex );
r.x += header.getDraggedDistance();
Graphics2D g2 = (Graphics2D) g.create();
try {
FlatUIUtils.setRenderingHints( g2 );
// paint dragged bottom line
g2.setColor( bottomSeparatorColor );
g2.fill( new Rectangle2D.Float( r.x, r.y + r.height - lineWidth, r.width, lineWidth ) );
// paint dragged column separator lines
g2.setColor( separatorColor );
g2.fill( new Rectangle2D.Float( r.x, topLineIndent, lineWidth, r.height - bottomLineIndent ) );
g2.fill( new Rectangle2D.Float( r.x + r.width - lineWidth, r.y + topLineIndent, lineWidth, r.height - bottomLineIndent ) );
} finally {
g2.dispose();
}
}
private void paintCells(SynthContext context, Graphics g, int rMin,
int rMax, int cMin, int cMax) {
JTableHeader header = table.getTableHeader();
TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn();
TableColumnModel cm = table.getColumnModel();
int columnMargin = cm.getColumnMargin();
Rectangle cellRect;
TableColumn aColumn;
int columnWidth;
if (table.getComponentOrientation().isLeftToRight()) {
for(int row = rMin; row <= rMax; row++) {
cellRect = table.getCellRect(row, cMin, false);
for(int column = cMin; column <= cMax; column++) {
aColumn = cm.getColumn(column);
columnWidth = aColumn.getWidth();
cellRect.width = columnWidth - columnMargin;
if (aColumn != draggedColumn) {
paintCell(context, g, cellRect, row, column);
}
cellRect.x += columnWidth;
}
}
} else {
for(int row = rMin; row <= rMax; row++) {
cellRect = table.getCellRect(row, cMin, false);
aColumn = cm.getColumn(cMin);
if (aColumn != draggedColumn) {
columnWidth = aColumn.getWidth();
cellRect.width = columnWidth - columnMargin;
paintCell(context, g, cellRect, row, cMin);
}
for(int column = cMin+1; column <= cMax; column++) {
aColumn = cm.getColumn(column);
columnWidth = aColumn.getWidth();
cellRect.width = columnWidth - columnMargin;
cellRect.x -= columnWidth;
if (aColumn != draggedColumn) {
paintCell(context, g, cellRect, row, column);
}
}
}
}
// Paint the dragged column if we are dragging.
if (draggedColumn != null) {
paintDraggedArea(context, g, rMin, rMax, draggedColumn, header.getDraggedDistance());
}
// Remove any renderers that may be left in the rendererPane.
rendererPane.removeAll();
}
PerGroupDependencyTableModel(JBTable perGroupDependencyTable,
@NotNull ProjectCreationRequest request, @NotNull DependencyGroup dependencyGroup,
@NotNull Version bootVersion,
@NotNull Map<DependencyGroup, List<Dependency>> filteredGroupAndDependencies) {
this.perGroupDependencyTable = perGroupDependencyTable;
this.request = request;
this.dependencyGroup = dependencyGroup;
this.bootVersion = bootVersion;
this.filteredGroupAndDependencies = filteredGroupAndDependencies;
reindex();
perGroupDependencyTable.setModel(this);
resetTableLookAndFeelToSingleSelect(perGroupDependencyTable);
TableColumnModel columnModel = perGroupDependencyTable.getColumnModel();
columnModel.setColumnMargin(0);
TableColumn checkBoxColumn = columnModel.getColumn(CHECKBOX_COL_INDEX);
TableUtil.setupCheckboxColumn(checkBoxColumn);
checkBoxColumn.setCellRenderer(new BooleanTableCellRenderer());
TableColumn dependencyColumn = columnModel.getColumn(DEPENDENCY_NAME_COL_INDEX);
dependencyColumn.setCellRenderer(new ColoredTableCellRenderer() {
@Override
protected void customizeCellRenderer(JTable table, @Nullable Object value, boolean selected,
boolean hasFocus, int row, int column) {
if (value != null) {
Dependency dependency = Dependency.class.cast(value);
boolean selectable = isCellEditable(row, CHECKBOX_COL_INDEX);
if (selectable) {
append(dependency.getName());
} else {
append(dependency.getName(), GRAY_ATTRIBUTES);
}
}
// Enable search highlighting. This in conjunction with TableSpeedSearch(below) enables type to search capability of intellij
applySpeedSearchHighlighting(table, this, true, selected);
}
});
new TableSpeedSearch(perGroupDependencyTable, value -> {
if (value instanceof Dependency) {
return Dependency.class.cast(value).getName();
}
return "";
});
// Add listeners
// Allow user to select via keyboard
perGroupDependencyTable.getActionMap().put("select_deselect_dependency", new AbstractAction() {
public void actionPerformed(ActionEvent e) {
toggleSelectionIfApplicable(perGroupDependencyTable.getSelectedRow());
}
});
perGroupDependencyTable.getInputMap()
.put(getKeyStroke(VK_SPACE, 0), "select_deselect_dependency");
// Allow user to toggle via double click
perGroupDependencyTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent event) {
if (event.getButton() == BUTTON1) {
int columIndex = perGroupDependencyTable.columnAtPoint(event.getPoint());
if (columIndex == DEPENDENCY_NAME_COL_INDEX) {
int rowIndex = perGroupDependencyTable.rowAtPoint(event.getPoint());
if (event.getClickCount() == 2) {
toggleSelectionIfApplicable(rowIndex);
}
}
}
}
});
perGroupDependencyTable.getSelectionModel().addListSelectionListener(e -> {
if (!e.getValueIsAdjusting()) {
int selectedRow = perGroupDependencyTable.getSelectedRow();
if (selectedRow != -1) {
selectionListeners
.forEach(listener -> listener.onDependencySelected(getDependencyAt(selectedRow)));
} else {
selectionListeners.forEach(listener -> listener.onDependencySelected(null));
}
}
});
}
private void create() {
model = new FindCheckoutsTableModel(folder, plugin.getTool());
model.addInitialLoadListener(new ThreadedTableModelListener() {
@Override
public void loadPending() {
// don't care
}
@Override
public void loadingStarted() {
// don't care
}
@Override
public void loadingFinished(boolean wasCancelled) {
if (wasCancelled) {
setStatusText("Find Checkouts Cancelled");
return;
}
boolean hasData = model.getRowCount() > 0;
if (!hasData && showMessage) {
Msg.showInfo(getClass(), threadedTablePanel, "Find Checkouts",
"No checkouts were found.");
FindCheckoutsDialog.this.close();
}
}
});
threadedTablePanel = new GThreadedTablePanel<>(model);
table = threadedTablePanel.getTable();
TableColumnModel columnModel = table.getColumnModel();
MyCellRenderer cellRenderer = new MyCellRenderer();
for (int i = 0; i < columnModel.getColumnCount(); i++) {
TableColumn column = columnModel.getColumn(i);
column.setCellRenderer(cellRenderer);
String name = (String) column.getIdentifier();
if (name.equals(FindCheckoutsTableModel.CHECKOUT_DATE)) {
column.setPreferredWidth(180);
}
}
table.setPreferredScrollableViewportSize(
new Dimension(threadedTablePanel.getPreferredSize().width, 150));
addWorkPanel(threadedTablePanel);
addDismissButton();
}