下面列出了javax.swing.table.TableColumn#getMinWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Return the minimum size of the table. The minimum height is the row height times the number of rows. The minimum
* width is the sum of the minimum widths of each column.
*/
public Dimension getMinimumSize(final JComponent c) {
long width = 0;
Enumeration<TableColumn> enumeration = table.getColumnModel().getColumns();
while (enumeration.hasMoreElements()) {
TableColumn aColumn = enumeration.nextElement();
width = width + aColumn.getMinWidth();
}
return createTableSize(width);
}
public void copyValues(TableColumn base) {
modelIndex = base.getModelIndex();
identifier = base.getIdentifier();
width = base.getWidth();
minWidth = base.getMinWidth();
setPreferredWidth(base.getPreferredWidth());
maxWidth = base.getMaxWidth();
headerRenderer = base.getHeaderRenderer();
headerValue = base.getHeaderValue();
cellRenderer = base.getCellRenderer();
cellEditor = base.getCellEditor();
isResizable = base.getResizable();
}
public void copyValues(TableColumn base) {
modelIndex = base.getModelIndex();
identifier = base.getIdentifier();
width = base.getWidth();
minWidth = base.getMinWidth();
setPreferredWidth(base.getPreferredWidth());
maxWidth = base.getMaxWidth();
headerRenderer = base.getHeaderRenderer();
headerValue = base.getHeaderValue();
cellRenderer = base.getCellRenderer();
cellEditor = base.getCellEditor();
isResizable = base.getResizable();
}
public void adjustTable(Component tableContainer, JTable table) {
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
DefaultTableColumnModel defColumnModel = (DefaultTableColumnModel) table.getColumnModel();
int columnCount = table.getColumnCount();
int[] columnWidths = new int[columnCount];
int totalWidth = 0;
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
TableColumn tableColumn = defColumnModel.getColumn(columnIndex);
TableCellRenderer headerRenderer = tableColumn.getHeaderRenderer();
if (headerRenderer == null) {
headerRenderer = table.getTableHeader().getDefaultRenderer();
}
int minWidth = tableColumn.getMinWidth();
int headerWidth = headerRenderer.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false, false, -1, columnIndex).getPreferredSize().width;
int finalColumnWidth = Math.max(minWidth, headerWidth);
for (int rowIndex = 0; rowIndex < table.getRowCount(); rowIndex++) {
int rowWidth = table.prepareRenderer(table.getCellRenderer(rowIndex, columnIndex), rowIndex, columnIndex).getPreferredSize().width;
finalColumnWidth = Math.max(rowWidth, finalColumnWidth);
// Object cellValue = table.getValueAt(rowIndex, columnIndex);
// try {
// long longValue = (Long) cellValue;
// cellTextField.setHorizontalAlignment(JTextField.RIGHT);
// } catch (Exception e0) {
// try {
// double doubleValue = (Double) cellValue;
// cellTextField.setHorizontalAlignment(JTextField.RIGHT);
// } catch (Exception e1) {
// try {
// boolean doubleValue = (Boolean) cellValue;
// cellTextField.setHorizontalAlignment(JTextField.CENTER);
// } catch (Exception e2) {
// cellTextField.setHorizontalAlignment(JTextField.LEADING);
// }
// }
// }
}
columnWidths[columnIndex] = finalColumnWidth;
totalWidth += columnWidths[columnIndex];
}
int containerWidth = tableContainer.getWidth();
if (totalWidth < containerWidth) {
TableColumnModel columnModel = table.getColumnModel();
int rest = containerWidth - totalWidth;
int additionalWidth = rest / columnCount;
int extraWidth = rest % columnCount;
int maxWidthColumnIndex = 0;
int maxColumnWidth = 0;
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
int columnWidth = columnWidths[columnIndex];
if (maxColumnWidth < columnWidth) {
maxColumnWidth = columnWidth;
maxWidthColumnIndex = columnIndex;
}
}
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
columnModel.getColumn(columnIndex).setPreferredWidth(columnWidths[columnIndex] + additionalWidth + (columnIndex == maxWidthColumnIndex ? extraWidth : 0));
}
}
}
protected void loadDataViewer( QueryResult queryResult ) {
if (queryResult == null) {
currentDataTable.setModel(new DefaultTableModel());
return;
}
Object[] names = queryResult.names.toArray(new String[0]);
List<Object[]> data = queryResult.data;
Object[][] values = new Object[queryResult.data.size()][];
int index = 0;
for( Object[] objects : data ) {
values[index++] = objects;
for( int i = 0; i < objects.length; i++ ) {
if (objects[i] instanceof Date) {
Date date = (Date) objects[i];
String formatted = DbsUtilities.dbDateFormatter.format(date);
objects[i] = formatted;
}
}
}
currentDataTable.setModel(new DefaultTableModel(values, names));
currentDataTable.setCellSelectionEnabled(true);
for( int column = 0; column < currentDataTable.getColumnCount(); column++ ) {
TableColumn tableColumn = currentDataTable.getColumnModel().getColumn(column);
int preferredWidth = tableColumn.getMinWidth();
int maxWidth = tableColumn.getMaxWidth();
for( int row = 0; row < currentDataTable.getRowCount(); row++ ) {
TableCellRenderer cellRenderer = currentDataTable.getCellRenderer(row, column);
Component c = currentDataTable.prepareRenderer(cellRenderer, row, column);
int width = c.getPreferredSize().width + currentDataTable.getIntercellSpacing().width;
preferredWidth = Math.max(preferredWidth, width);
if (preferredWidth >= maxWidth) {
preferredWidth = maxWidth;
break;
}
}
if (preferredWidth < 50) {
preferredWidth = 50;
}
if (preferredWidth > 300) {
preferredWidth = 300;
}
tableColumn.setPreferredWidth(preferredWidth);
}
_recordCountTextfield.setText(values.length + " in " + millisToTimeString(queryResult.queryTimeMillis));
}