Compute optimal column width if not in preference
This commit is contained in:
parent
bec89c444b
commit
78dac7e498
@ -156,10 +156,10 @@ public class ComponentPresetChooserDialog extends JDialog {
|
||||
// need to create componentSelectionTable before filter checkboxes,
|
||||
// but add to panel after
|
||||
componentSelectionTable = new ComponentPresetTable(presetType, presets, displayedColumnKeys);
|
||||
GUIUtil.setAutomaticColumnTableWidths(componentSelectionTable, 20);
|
||||
|
||||
// Make the first column (the favorite column) as small as possible
|
||||
int w = componentSelectionTable.getRowHeight() + 4;
|
||||
XTableColumnModel tm = componentSelectionTable.getXColumnModel();
|
||||
//TableColumn tc = componentSelectionTable.getColumnModel().getColumn(0);
|
||||
TableColumn tc = tm.getColumn(0);
|
||||
tc.setPreferredWidth(w);
|
||||
tc.setMaxWidth(w);
|
||||
@ -168,8 +168,15 @@ public class ComponentPresetChooserDialog extends JDialog {
|
||||
// The normal left/right and tab/shift-tab key action traverses each cell/column of the table instead of going to the next row.
|
||||
TableRowTraversalPolicy.setTableRowTraversalPolicy(componentSelectionTable);
|
||||
|
||||
// Add checkboxes (legacy, match fore diameter, ...)
|
||||
panel.add(getFilterCheckboxes(tm, legacyColumnIndex), "wrap para");
|
||||
|
||||
// Load the table UI settings from the preferences
|
||||
boolean legacySelected = showLegacyCheckBox.isSelected();
|
||||
TableUIPreferences.loadTableUIPreferences(componentSelectionTable, TABLE_ID + component.getComponentName(),
|
||||
preferences.getTablePreferences());
|
||||
showLegacyCheckBox.setSelected(legacySelected); // Restore legacy state (may change during UI preference loading)
|
||||
|
||||
JScrollPane scrollpane = new JScrollPane();
|
||||
scrollpane.setViewportView(componentSelectionTable);
|
||||
panel.add(scrollpane, "grow, pushy, spanx, wrap rel");
|
||||
@ -219,9 +226,6 @@ public class ComponentPresetChooserDialog extends JDialog {
|
||||
this.setLocationByPlatform(true);
|
||||
GUIUtil.rememberWindowPosition(this);
|
||||
|
||||
TableUIPreferences.loadTableUIPreferences(componentSelectionTable, TABLE_ID + component.getComponentName(),
|
||||
preferences.getTablePreferences());
|
||||
|
||||
updateFilters();
|
||||
}
|
||||
|
||||
|
@ -58,6 +58,7 @@ import javax.swing.event.ChangeListener;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
import javax.swing.table.DefaultTableColumnModel;
|
||||
import javax.swing.table.DefaultTableModel;
|
||||
import javax.swing.table.TableCellRenderer;
|
||||
import javax.swing.table.TableColumn;
|
||||
import javax.swing.table.TableColumnModel;
|
||||
import javax.swing.table.TableModel;
|
||||
@ -448,25 +449,45 @@ public class GUIUtil {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void setAutomaticColumnTableWidths(JTable table, int max) {
|
||||
/**
|
||||
* Computes the optimal column widths for the specified table, based on the content in all rows for that column,
|
||||
* and optionally also the column header content.
|
||||
* @param table the table
|
||||
* @param max the maximum width for a column
|
||||
* @param includeHeaderWidth whether to include the width of the column header
|
||||
* @return an array of column widths
|
||||
*/
|
||||
public static int[] computeOptimalColumnWidths(JTable table, int max, boolean includeHeaderWidth) {
|
||||
int columns = table.getColumnCount();
|
||||
int widths[] = new int[columns];
|
||||
int[] widths = new int[columns];
|
||||
Arrays.fill(widths, 1);
|
||||
|
||||
for (int row = 0; row < table.getRowCount(); row++) {
|
||||
// Consider the width required by the header text if includeHeaderWidth is true
|
||||
if (includeHeaderWidth) {
|
||||
TableCellRenderer headerRenderer = table.getTableHeader().getDefaultRenderer();
|
||||
for (int col = 0; col < columns; col++) {
|
||||
Object value = table.getValueAt(row, col);
|
||||
//System.out.println("row=" + row + " col=" + col + " : " + value);
|
||||
widths[col] = Math.max(widths[col], value == null ? 0 : value.toString().length());
|
||||
TableColumn column = table.getColumnModel().getColumn(col);
|
||||
Component headerComp = headerRenderer.getTableCellRendererComponent(table, column.getHeaderValue(), false, false, 0, col);
|
||||
widths[col] = headerComp.getPreferredSize().width;
|
||||
}
|
||||
}
|
||||
|
||||
// Compare header width to the width required by the cell data
|
||||
for (int row = 0; row < table.getRowCount(); row++) {
|
||||
for (int col = 0; col < columns; col++) {
|
||||
Object value = table.getValueAt(row, col);
|
||||
TableCellRenderer cellRenderer = table.getCellRenderer(row, col);
|
||||
Component cellComp = cellRenderer.getTableCellRendererComponent(table, value, false, false, row, col);
|
||||
int cellWidth = cellComp.getPreferredSize().width;
|
||||
widths[col] = Math.max(widths[col], cellWidth);
|
||||
}
|
||||
}
|
||||
|
||||
for (int col = 0; col < columns; col++) {
|
||||
//System.err.println("Setting column " + col + " to width " + widths[col]);
|
||||
table.getColumnModel().getColumn(col).setPreferredWidth(Math.min(widths[col], max) * 100);
|
||||
widths[col] = Math.min(widths[col], max * 100); // Adjusting for your max value and scaling
|
||||
}
|
||||
|
||||
return widths;
|
||||
}
|
||||
|
||||
public static Window getWindowAncestor(Component c) {
|
||||
|
@ -69,10 +69,25 @@ public class TableUIPreferences {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if any column width is missing from preferences
|
||||
boolean computeOptimalWidths = false;
|
||||
for (int i = 0; i < table.getColumnCount() && !computeOptimalWidths; i++) {
|
||||
TableColumn column = table.getColumnModel().getColumn(i);
|
||||
if (preferences.get(tableName + TABLE_COLUMN_WIDTH_PREFIX + column.getIdentifier(), null) == null) {
|
||||
computeOptimalWidths = true;
|
||||
}
|
||||
}
|
||||
|
||||
// If any column width is missing, compute optimal widths for all columns
|
||||
int[] optimalWidths = null;
|
||||
if (computeOptimalWidths) {
|
||||
optimalWidths = GUIUtil.computeOptimalColumnWidths(table, 20, true);
|
||||
}
|
||||
|
||||
// Restore column widths
|
||||
for (int i = 0; i < table.getColumnCount(); i++) {
|
||||
TableColumn column = table.getColumnModel().getColumn(i);
|
||||
int defaultWidth = column.getWidth();
|
||||
int defaultWidth = (optimalWidths != null) ? optimalWidths[i] : column.getWidth();
|
||||
int width = preferences.getInt(tableName + TABLE_COLUMN_WIDTH_PREFIX + column.getIdentifier(), defaultWidth);
|
||||
column.setPreferredWidth(width);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user