下面列出了javax.swing.JTable#prepareRenderer ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* This method resizes the columns
*/
private void resizeColumnWidth(JTable table) {
final TableColumnModel columnModel = table.getColumnModel();
for (int column = 0; column < table.getColumnCount(); column++) {
Object headerValue = columnModel.getColumn(column).getHeaderValue();
TableCellRenderer headerRenderer = columnModel.getColumn(column).getHeaderRenderer();
if (headerRenderer == null) {
headerRenderer = table.getTableHeader().getDefaultRenderer();
}
Component headerComp =
headerRenderer.getTableCellRendererComponent(table, headerValue, false, false, 0, column);
int width = 30; // Min width
width = Math.max(width, headerComp.getPreferredSize().width + 10);
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component comp = table.prepareRenderer(renderer, row, column);
width = Math.max(comp.getPreferredSize().width + 10, width);
}
columnModel.getColumn(column).setPreferredWidth(width);
}
}
/**
* From Stackoverflow: http://stackoverflow.com/a/17627497
*
* @param table
* The table to be resized
*/
public void resizeColumnWidth(JTable table) {
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
final TableColumnModel columnModel = table.getColumnModel();
for (int column = 0; column < table.getColumnCount(); column++) {
TableColumn tableColumn = columnModel.getColumn(column);
TableCellRenderer r = tableColumn.getHeaderRenderer();
if (r == null) {
r = table.getTableHeader().getDefaultRenderer();
}
Component component = r.getTableCellRendererComponent(table, tableColumn.getHeaderValue(), false, false, 0,
column);
int width = component.getPreferredSize().width;
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component comp = table.prepareRenderer(renderer, row, column);
width = Math.max(comp.getPreferredSize().width + 1, width);
}
tableColumn.setPreferredWidth(width);
}
}
@SuppressWarnings("serial")
public static JPanel makeTable(Border b, String border, Object[][] rowData, Object... colNames) {
JTable t = new JTable(rowData, colNames) {
@Override
public Dimension getPreferredScrollableViewportSize() {
Dimension d = getPreferredSize();
return new Dimension(Integer.max(640, d.width), (d.height));
}
};
JPanel p = new JPanel(new GridLayout(1, 1));
TableRowSorter<?> sorter = new MyTableRowSorter(t.getModel());
if (colNames.length > 0) {
sorter.toggleSortOrder(0);
}
t.setRowSorter(sorter);
sorter.allRowsChanged();
p.add(new JScrollPane(t));
for (int row = 0; row < t.getRowCount(); row++) {
int rowHeight = t.getRowHeight();
for (int column = 0; column < t.getColumnCount(); column++) {
Component comp = t.prepareRenderer(t.getCellRenderer(row, column), row, column);
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
}
t.setRowHeight(row, rowHeight);
}
Font font = UIManager.getFont("TableHeader.font");
p.setBorder(BorderFactory.createTitledBorder(b, border, TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, font, Color.black));
return p;
}
public void resizeColumnWidth(JTable table) {
final TableColumnModel columnModel = table.getColumnModel();
for (int column = 0; column < table.getColumnCount(); column++) {
int width = 50; // Min width
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component comp = table.prepareRenderer(renderer, row, column);
width = Math.max(comp.getPreferredSize().width, width);
}
columnModel.getColumn(column).setPreferredWidth(width);
columnModel.getColumn(column).setMinWidth(minColumnWidths[column]);
}
}
public void adjustColumnPreferredWidths(JTable table) {
// Gets max width for cells in column as the preferred width
TableColumnModel columnModel = table.getColumnModel();
for (int col = 0; col < table.getColumnCount(); col++) {
int width = 45;
for (int row = 0; row < table.getRowCount(); row++) {
if (tableCellRenderer == null)
tableCellRenderer = table.getCellRenderer(row, col);
Component comp = table.prepareRenderer(tableCellRenderer, row, col);
width = Math.max(comp.getPreferredSize().width, width);
}
columnModel.getColumn(col).setPreferredWidth(width);
}
}
/**
* Adjust the column widths of a table based on the table contents.
*
* @param table adjusted table
*/
private void adjustColumnWidths(JTable table) {
TableColumnModel model = table.getColumnModel();
for (int column = 0; column < table.getColumnCount(); column++) {
TableColumn tc = model.getColumn(column);
int width = tc.getWidth();
for (int row = 0; row < table.getRowCount(); row++) {
Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
width = Math.max(width, comp.getPreferredSize().width);
}
tc.setPreferredWidth(width);
}
}
/**
* Adjust the row heights of a table based on the table contents.
*
* @param table adjusted table
*/
private void adjustRowHeights(JTable table) {
for (int row = 0; row < table.getRowCount(); row++) {
int rowHeight = table.getRowHeight();
for (int column = 0; column < table.getColumnCount(); column++) {
Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
}
table.setRowHeight(row, rowHeight);
}
}
private void resizeColumnWidth(JTable table) {
final TableColumnModel columnModel = table.getColumnModel();
for (int column = 0; column < table.getColumnCount(); column++) {
int width = 50; // Min width
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer renderer = table.getCellRenderer(row, column);
Component comp = table.prepareRenderer(renderer, row, column);
width = Math.max(comp.getPreferredSize().width, width);
}
columnModel.getColumn(column).setPreferredWidth(width + 10);
}
}
private static int columnMaxWidth(@NotNull JTable table, int col) {
TableColumn column = table.getColumnModel().getColumn(col);
int width = 0;
for (int row = 0; row < table.getRowCount(); row++) {
Component component = table.prepareRenderer(column.getCellRenderer(), row, col);
int rendererWidth = component.getPreferredSize().width;
width = Math.max(width, rendererWidth + table.getIntercellSpacing().width);
}
return width;
}
private static int columnMaxWidth(@NotNull JTable table, int col) {
TableColumn column = table.getColumnModel().getColumn(col);
int width = 0;
for (int row = 0; row < table.getRowCount(); row++) {
Component component = table.prepareRenderer(column.getCellRenderer(), row, col);
int rendererWidth = component.getPreferredSize().width;
width = Math.max(width, rendererWidth + table.getIntercellSpacing().width);
}
return width;
}
public static void adjustColumnPreferredWidths(JTable table) {
// Gets max width for cells in column as the preferred width
TableColumnModel columnModel = table.getColumnModel();
for (int col=0; col<table.getColumnCount(); col++) {
// System.out.println ("--- col " + col + " ---");
int maxwidth = 40;
//int minwidth = 40;
int colWidth = 0;
for (int row=0; row<table.getRowCount(); row++) {
TableCellRenderer rend = table.getCellRenderer (row, col);
// Object value = table.getValueAt (row, col);
// Component comp =
// rend.getTableCellRendererComponent (table,
// value,
// false,
// false,
// row,
// col);
Component comp = table.prepareRenderer(rend, row, col);
colWidth = comp.getPreferredSize().width;
//minwidth = colWidth;
//if (colWidth > maxwidth) maxwidth = colWidth;
//if (colWidth < minwidth) minwidth = colWidth;
maxwidth = Math.max (colWidth, maxwidth);
//minwidth = Math.max (colWidth, minwidth);
//System.out.println ("col " + col +
// " pref width now " +
// maxwidth);
}
// Considers the column header's preferred width too
TableColumn column = columnModel.getColumn (col);
// int headerWidth = 0;
// TableCellRenderer headerRenderer = column.getHeaderRenderer();
// if (headerRenderer == null)
// headerRenderer = table.getTableHeader().getDefaultRenderer();
// Object headerValue = column.getHeaderValue();
// Component headerComp =
// headerRenderer.getTableCellRendererComponent (table,
// headerValue,
// false,
// false,
// 0,
// col);
// headerWidth = headerComp.getPreferredSize().width;
//
// maxwidth = Math.max (colWidth, headerWidth);
// minwidth = Math.min (colWidth, headerWidth);
column.setPreferredWidth (maxwidth);
//column.setMaxWidth(maxwidth); // very bad!
column.setMinWidth(maxwidth);
}
}