Package sc.fiji.snt.gui
Class GuiUtils.Tables
java.lang.Object
sc.fiji.snt.gui.GuiUtils.Tables
- Enclosing class:
GuiUtils
Utility methods for
JTable configuration and creation.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classStateful helper that extracts aJScrollPanefrom its current parent into a free-floating, modelessJDialog, and re-attaches it on demand. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidassignSearchable(JTable table, Function<Object, String> elementConverter, JPopupMenu menu) static voidautoSizeColumn(JTable table, int columnIndex, int padding) Auto-sizes the preferred width of the given column to fit its content, with padding.static voidstatic voidconfigureCheckboxColumn(JTable table, int columnIndex, String tooltip) Configures a Boolean column to render and edit as a checkbox, with an optional header tooltip.static voidcopyToClipboard(JTable table, boolean includeHeaders) static JMenuItemdeselectSelectAllMenuItem(JTable table, Runnable onChange) Common "Deselect / Select All" toggle menu item.static voidenhanceTableDisplay(SNTTable table, String windowTitle) Enhances a SciJava-managed table display with conveniences (close shortcut, save menu).static voidfreezeColumnWidth(JTable table, int columnIndex, int padding) Auto-sizes the given column to fit its content, then freezes it by setting preferred, minimum, and maximum width to the same value.static TableCellRenderericonHeaderRenderer(Icon icon, String tooltip) Creates a header renderer that displays a centered icon instead of text.static voidinstallAlternatingRows(JTable table) Applies a subtle alternating-row background tint to a table (zebra striping), without requiring a customJTablesubclass.static JMenuItemresetAndResizeColumnsMenuItem(JTable table, Runnable onAction, float... widthFractions) "Resize/Reset Columns" menu item that restores model column order and then applies width fractions.static voidresetColumnOrder(JTable table) Restores the table's column order to match the model order (in case the user dragged columns around).static voidresizeColumns(JTable table, float... widthFractions) Applies a sequence of preferred column widths to the table, each expressed as a fraction of the current total column width.static JScrollPanescrollPane(JTable table) Wraps a JTable in a JScrollPane with sensible defaults: auto-resize mode set to ALL_COLUMNS, row sorter enabled, and grid lines hidden.static JScrollPanescrollPane(JTable table, int autoResizeMode, boolean showGrid, boolean showHorizontalLines) Wraps a JTable in a JScrollPane, applying common configuration.static voidscrollToBottom(JTable table) Scrolls a JTable to show its last row.static JTabletableWithPlaceholder(TableModel model, Supplier<String> line1, Supplier<String> line2) Returns a JTable subclass that draws centered placeholder text when the table model is empty.
-
Method Details
-
scrollPane
Wraps a JTable in a JScrollPane with sensible defaults: auto-resize mode set to ALL_COLUMNS, row sorter enabled, and grid lines hidden.- Parameters:
table- the table to wrap- Returns:
- a configured JScrollPane containing the table
-
scrollPane
public static JScrollPane scrollPane(JTable table, int autoResizeMode, boolean showGrid, boolean showHorizontalLines) Wraps a JTable in a JScrollPane, applying common configuration. Auto-row-sorter is enabled, the table fills the viewport, and intercell spacing is set to zero.- Parameters:
table- the table to wrapautoResizeMode- one of theJTable.AUTO_RESIZE_*constantsshowGrid- whether to show grid linesshowHorizontalLines- whether to show horizontal lines- Returns:
- a configured JScrollPane containing the table
-
iconHeaderRenderer
Creates a header renderer that displays a centered icon instead of text. Inherits the default header foreground, background, font, and border so that the column blends with the rest of the table header.- Parameters:
icon- the icon to displaytooltip- the tooltip for the header cell (may benull)- Returns:
- a configured
TableCellRenderer
-
configureCheckboxColumn
Configures a Boolean column to render and edit as a checkbox, with an optional header tooltip.- Parameters:
table- the tablecolumnIndex- the column indextooltip- tooltip for the column header (null to skip)
-
installAlternatingRows
Applies a subtle alternating-row background tint to a table (zebra striping), without requiring a customJTablesubclass. Wraps the default renderer registered for each of the table's column classes so that unselected rows alternate between the table's own background and a slight tint towards its foreground color.Only affects columns that rely on the class-based default renderer (the normal case). A column with a renderer assigned directly via
TableColumn.setCellRenderer(javax.swing.table.TableCellRenderer)bypasses the class-based lookup and is left untouched by this method.- Parameters:
table- the table to stripe
-
autoSizeColumn
Auto-sizes the preferred width of the given column to fit its content, with padding.- Parameters:
table- the tablecolumnIndex- the column indexpadding- extra pixels of padding per side
-
copyToClipboard
-
assignSearchable
public static void assignSearchable(JTable table, Function<Object, String> elementConverter, JPopupMenu menu) -
freezeColumnWidth
Auto-sizes the given column to fit its content, then freezes it by setting preferred, minimum, and maximum width to the same value.- Parameters:
table- the tablecolumnIndex- the column indexpadding- extra pixels of padding
-
tableWithPlaceholder
public static JTable tableWithPlaceholder(TableModel model, Supplier<String> line1, Supplier<String> line2) Returns a JTable subclass that draws centered placeholder text when the table model is empty. Two lines are supported: a primary line and an optional secondary line rendered directly below it. Suppliers are used so that placeholder text can change dynamically (e.g., when filters are applied).- Parameters:
model- the table modelline1- supplier for the primary placeholder text (nevernull)line2- supplier for the secondary placeholder text (may returnnull)- Returns:
- a JTable that paints the placeholder when it has no rows
-
scrollToBottom
Scrolls a JTable to show its last row.This implementation avoids using
JTable.getCellRect(int, int, boolean)with row indices. That can trigger "row index is bigger than sorter's row count" warnings when the table'sRowSorterhasn't fully synchronized. Instead, we manipulate the viewport.The double
SwingUtilities.invokeLater(java.lang.Runnable)ensures that:- 1st pass:
revalidate()triggers layout recalculation for any newly added rows - 2nd pass: scrolling occurs after layout is complete, so
table.getHeight()reflects the actual table size including new rows
- Parameters:
table- the table to scroll (must be inside aJScrollPane)
- 1st pass:
-
enhanceTableDisplay
Enhances a SciJava-managed table display with conveniences (close shortcut, save menu). Fails silently if the display cannot be found or enhanced.- Parameters:
table- the SNTTable being displayedwindowTitle- the title of the display window to find
-
closeAllTables
public static void closeAllTables() -
deselectSelectAllMenuItem
Common "Deselect / Select All" toggle menu item. Clears the selection if any rows are selected; otherwise selects every row.- Parameters:
table- the table whose selection is toggledonChange- optional callback invoked after the selection mutates (e.g., for recording / scripting); may benull
-
resetColumnOrder
Restores the table's column order to match the model order (in case the user dragged columns around). -
resizeColumns
Applies a sequence of preferred column widths to the table, each expressed as a fraction of the current total column width. If fewer fractions are supplied than the table has columns, the trailing columns are left untouched.- Parameters:
table- the table to resizewidthFractions- fractions of the total column width (e.g.0.10f, 0.20f, …); ignored whennullor empty
-
resetAndResizeColumnsMenuItem
public static JMenuItem resetAndResizeColumnsMenuItem(JTable table, Runnable onAction, float... widthFractions) "Resize/Reset Columns" menu item that restores model column order and then applies width fractions.- Parameters:
table- the table to act ononAction- optional callback fired after the operation (e.g., for recording); may benullwidthFractions- fractions used by the resize step; pass an empty array to only restore column order
-