下面列出了javax.swing.table.TableColumn#getMaxWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Return the maximum size of the table. The maximum height is the row heighttimes the number of rows. The maximum
* width is the sum of the maximum widths of each column.
*/
public Dimension getMaximumSize(final JComponent c) {
long width = 0;
Enumeration<TableColumn> enumeration = table.getColumnModel().getColumns();
while (enumeration.hasMoreElements()) {
TableColumn aColumn = enumeration.nextElement();
width = width + aColumn.getMaxWidth();
}
return createTableSize(width);
}
public static void updateColumnWidths(JTable table) {
double pw = table.getParent().getSize().getWidth();
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
TableColumn column = table.getColumnModel().getColumn(1);
int w = ((int) pw / 2) - 1;
if (w > column.getMaxWidth()) {
w = column.getMaxWidth();
}
column.setWidth(w);
column.setPreferredWidth(w);
w = (int) pw - w;
column = table.getColumnModel().getColumn(0);
column.setWidth(w);
column.setPreferredWidth(w);
}
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();
}
/**
* Adapte les largeurs des colonnes selon les données de cette table. <br/>
* Pour chaque colonne la taille préférée est déterminée selon la valeur (et le renderer) du header et selon la valeur (et le renderer) de chaque cellule.
*/
public void adjustColumnWidths() {
if (ADJUST_COLUMN_WIDTHS_MAX_ROWS > 0) {
TableColumn tableColumn;
TableCellRenderer renderer;
Object value;
Component component;
final int columnCount = getColumnCount();
final int rowCount = Math.min(getRowCount(), ADJUST_COLUMN_WIDTHS_MAX_ROWS);
int columnWidth;
final int maxWidth = 250; // taille ajustée maximum (15 minimum par défaut)
// Boucle sur chaque colonne et chaque ligne.
// Trouve le max de la largeur du header et de chaque cellule
// et fixe la largeur de la colonne en fonction.
for (int colNum = 0; colNum < columnCount; colNum++) {
tableColumn = columnModel.getColumn(colNum);
if (tableColumn.getMaxWidth() <= 0) {
continue; // colonne invisible
}
renderer = getTableHeader().getDefaultRenderer();
value = tableColumn.getHeaderValue();
component = renderer.getTableCellRendererComponent(this, value, false, false, -1,
colNum);
columnWidth = component.getPreferredSize().width + 10;
renderer = getCellRenderer(-1, colNum);
for (int rowNum = 0; rowNum < rowCount; rowNum++) {
value = getValueAt(rowNum, colNum);
component = renderer.getTableCellRendererComponent(this, value, false, false,
rowNum, colNum);
columnWidth = Math.max(columnWidth, component.getPreferredSize().width);
}
columnWidth = Math.min(maxWidth, columnWidth);
tableColumn.setPreferredWidth(columnWidth + getIntercellSpacing().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();
}
boolean isColumnVisible(TableColumn column) {
return column.getMaxWidth() > 0;
}
boolean isColumnVisible(TableColumn column) {
return column.getMaxWidth() > 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));
}