lphystudio.core.swing.TableColumnAdjuster Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lphy-studio Show documentation
Show all versions of lphy-studio Show documentation
The GUI for LPhy language.
The newest version!
package lphystudio.core.swing;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import java.awt.*;
/**
* Table Column Adjuster
* https://tips4java.wordpress.com/2008/11/10/table-column-adjuster/
*/
public class TableColumnAdjuster {
public TableColumnAdjuster(JTable table) {
// this makes auto adjust working
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
for (int column = 0; column < table.getColumnCount(); column++) {
TableColumn tableColumn = table.getColumnModel().getColumn(column);
int preferredWidth = tableColumn.getMinWidth();
int maxWidth = tableColumn.getMaxWidth();
for (int row = 0; row < table.getRowCount(); row++) {
TableCellRenderer cellRenderer = table.getCellRenderer(row, column);
Component c = table.prepareRenderer(cellRenderer, row, column);
int width = c.getPreferredSize().width + table.getIntercellSpacing().width;
preferredWidth = Math.max(preferredWidth, width);
// We've exceeded the maximum width, no need to check other rows
if (preferredWidth >= maxWidth) {
preferredWidth = maxWidth;
break;
}
}
tableColumn.setPreferredWidth(preferredWidth);
}
}
}