Class GuiUtils.Tables

java.lang.Object
sc.fiji.snt.gui.GuiUtils.Tables
Enclosing class:
GuiUtils

public static class GuiUtils.Tables extends Object
Utility methods for JTable configuration and creation.
  • Method Details

    • scrollPane

      public static JScrollPane scrollPane(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.
      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 wrap
      autoResizeMode - one of the JTable.AUTO_RESIZE_* constants
      showGrid - whether to show grid lines
      showHorizontalLines - whether to show horizontal lines
      Returns:
      a configured JScrollPane containing the table
    • iconHeaderRenderer

      public static TableCellRenderer iconHeaderRenderer(Icon icon, String tooltip)
      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 display
      tooltip - the tooltip for the header cell (may be null)
      Returns:
      a configured TableCellRenderer
    • configureCheckboxColumn

      public static void configureCheckboxColumn(JTable table, int columnIndex, String tooltip)
      Configures a Boolean column to render and edit as a checkbox, with an optional header tooltip.
      Parameters:
      table - the table
      columnIndex - the column index
      tooltip - tooltip for the column header (null to skip)
    • installAlternatingRows

      public static void installAlternatingRows(JTable table)
      Applies a subtle alternating-row background tint to a table (zebra striping), without requiring a custom JTable subclass. 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

      public static void autoSizeColumn(JTable table, int columnIndex, int padding)
      Auto-sizes the preferred width of the given column to fit its content, with padding.
      Parameters:
      table - the table
      columnIndex - the column index
      padding - extra pixels of padding per side
    • copyToClipboard

      public static void copyToClipboard(JTable table, boolean includeHeaders)
    • assignSearchable

      public static void assignSearchable(JTable table, Function<Object,String> elementConverter, JPopupMenu menu)
    • freezeColumnWidth

      public static void freezeColumnWidth(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.
      Parameters:
      table - the table
      columnIndex - the column index
      padding - 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 model
      line1 - supplier for the primary placeholder text (never null)
      line2 - supplier for the secondary placeholder text (may return null)
      Returns:
      a JTable that paints the placeholder when it has no rows
    • scrollToBottom

      public static void scrollToBottom(JTable table)
      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's RowSorter hasn't fully synchronized. Instead, we manipulate the viewport.

      The double SwingUtilities.invokeLater(java.lang.Runnable) ensures that:

      1. 1st pass: revalidate() triggers layout recalculation for any newly added rows
      2. 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 a JScrollPane)
    • enhanceTableDisplay

      public static void enhanceTableDisplay(SNTTable table, String windowTitle)
      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 displayed
      windowTitle - the title of the display window to find
    • closeAllTables

      public static void closeAllTables()
    • deselectSelectAllMenuItem

      public static JMenuItem deselectSelectAllMenuItem(JTable table, Runnable onChange)
      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 toggled
      onChange - optional callback invoked after the selection mutates (e.g., for recording / scripting); may be null
    • resetColumnOrder

      public static void resetColumnOrder(JTable table)
      Restores the table's column order to match the model order (in case the user dragged columns around).
    • resizeColumns

      public static void resizeColumns(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. If fewer fractions are supplied than the table has columns, the trailing columns are left untouched.
      Parameters:
      table - the table to resize
      widthFractions - fractions of the total column width (e.g. 0.10f, 0.20f, …); ignored when null or 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 on
      onAction - optional callback fired after the operation (e.g., for recording); may be null
      widthFractions - fractions used by the resize step; pass an empty array to only restore column order