Class SwingUtil

java.lang.Object
com.dua3.utility.swing.SwingUtil

public final class SwingUtil extends Object
Utility methods for Swing applications.
  • Method Details

    • createAction

      public static Action createAction​(String name, Consumer<? super ActionEvent> onActionPerformed)
      Create an action to be used in menus.
      Parameters:
      name - the name to display
      onActionPerformed - the Consumer that gets called when the Action is invoked
      Returns:
      new Action instance
    • createAction

      public static Action createAction​(String name, Runnable onActionPerformed)
      Create an action to be used in menus.
      Parameters:
      name - the name to display
      onActionPerformed - the Runnable that gets called when the Action is invoked
      Returns:
      new Action instance
    • scrollToBottom

      public static void scrollToBottom​(JScrollPane sp)
      Scroll pane to bottom.
      Parameters:
      sp - a scroll pane
    • scrollToEnd

      public static void scrollToEnd​(JScrollBar sb)
      Scroll scrollbar to end.
      Parameters:
      sb - a scroll bar
    • setClipboardText

      public static void setClipboardText​(String text)
      Set clipboard content.
      Parameters:
      text - the text to set
    • setNativeLookAndFeel

      public static void setNativeLookAndFeel()
      Set the Swing Look&Feel to the native Look&Feel. On Mac OS, the global menubar is also enabled.
    • setNativeLookAndFeel

      public static void setNativeLookAndFeel​(String applicationName)
      Set the Swing Look&Feel to the native Look&Feel. On Mac OS, the global menubar is also enabled.
      Parameters:
      applicationName - the application name to set
    • showDirectoryOpenDialog

      public static Optional<Path> showDirectoryOpenDialog​(Component parent, Path current)
      Show directory open dialog.
      Parameters:
      parent - the parent component for the dialog
      current - the current file, it determines the folder shown when the dialog opens
      Returns:
      Optional containing the path to the selected file.
    • showFileOpenDialog

      @SafeVarargs public static Optional<Path> showFileOpenDialog​(Component parent, Path current, com.dua3.utility.data.Pair<String,​String[]>... types)
      Show file open dialog.
      Parameters:
      parent - the parent component for the dialog
      current - the current file, it determines the folder shown when the dialog opens
      types - the selectable file name filters, given as pairs of description and one or more extensions
      Returns:
      Optional containing the path to the selected file.
    • showFileSaveDialog

      @SafeVarargs public static Optional<Path> showFileSaveDialog​(Component parent, Path current, com.dua3.utility.data.Pair<String,​String[]>... types)
      Show file save dialog.
      Parameters:
      parent - the parent component for the dialog
      current - the current file, it determines the folder shown when the dialog opens
      types - the selectable file name filters, given as pairs of description and one or more extensions
      Returns:
      Optional containing the path to the selected file.
    • toColor

      public static com.dua3.utility.data.Color toColor​(Color color)
      Convert java.awt.Color to Color.
      Parameters:
      color - java.awt.Color to be converted
      Returns:
      Color
    • toAwtColor

      public static Color toAwtColor​(com.dua3.utility.data.Color color)
      Convert Color to java.awt.Color.
      Parameters:
      color - Color to be converted
      Returns:
      java.awt.Color
    • toAwtColor

      public static Color toAwtColor​(String s)
      Convert String to java.awt.Color.
      Parameters:
      s - String to be converted
      Returns:
      java.awt.Color
      See Also:
      Color.valueOf(String), toAwtColor(Color)
    • updateAndScrollToBottom

      public static void updateAndScrollToBottom​(JScrollPane sp, Runnable update)
      Execute update and scroll to bottom.

      If the JScrollPane given as parameter is scrolled to the bottom prior to performing the update, it will be scrolled to the bottom after executing the update. Otherwise, only the update is performed.

      Parameters:
      sp - a scroll pane
      update - Runnable to do the update
    • updateAndScrollToEnd

      public static void updateAndScrollToEnd​(JScrollBar sb, Runnable update)
      Execute update and scroll to end.

      If the JScrollBar given as parameter is scrolled to the end prior to performing the update, it will be scrolled to the end again after executing the update. Otherwise, only the update is performed.

      Parameters:
      sb - a scroll bar
      update - Runnable to do the update
    • showOpenDialog

      @SafeVarargs public static Optional<Path> showOpenDialog​(Component parent, Path current, int selectionMode, com.dua3.utility.data.Pair<String,​String[]>... types)
      Show open dialog.
      Parameters:
      parent - the parent component for the dialog
      current - the current path selected when the dialohg opens
      selectionMode - the selection mode as used in JFileChooser.setFileSelectionMode(int)
      types - pairs(desription, list of extensions) used to create FileNameExtensionFilter instances
      Returns:
      an Optional holding the selected path or an empty Optional if nothing was selected
    • setUnitIncrement

      public static void setUnitIncrement​(JScrollBar sb, int unitIncrement)
      Set the unit increment.
      Parameters:
      sb - the scroll bar, null is allowed
      unitIncrement - the unit increment
    • setUnitIncrement

      public static void setUnitIncrement​(JScrollPane jsp, int unitIncrement)
      Set the unit increment.
      Parameters:
      jsp - the scroll pane
      unitIncrement - the unit increment
    • createJScrollPane

      public static JScrollPane createJScrollPane​(int unitIncrement)
      Create JScrollPane with unit increment set.
      Parameters:
      unitIncrement - the unit increment
      Returns:
      new JScrollPane
    • createJScrollPane

      public static JScrollPane createJScrollPane​(int unitIncrement, Component view)
      Create JScrollPane with unit increment set.
      Parameters:
      unitIncrement - the unit increment
      view - the view
      Returns:
      new JScrollPane
    • createJScrollPane

      public static JScrollPane createJScrollPane​(int unitIncrement, int vsbPolicy, int hsbPolicy)
      Create JScrollPane with unit increment set.
      Parameters:
      unitIncrement - the unit increment
      vsbPolicy - vertical scroll bar policy
      hsbPolicy - horizontal scroll bar policy
      Returns:
      new JScrollPane
    • createJScrollPane

      public static JScrollPane createJScrollPane​(int unitIncrement, Component view, int vsbPolicy, int hsbPolicy)
      Create JScrollPane with unit increment set.
      Parameters:
      unitIncrement - the unit increment
      view - the view
      vsbPolicy - vertical scroll bar policy
      hsbPolicy - horizontal scroll bar policy
      Returns:
      new JScrollPane
    • addDropFilesSupport

      public static void addDropFilesSupport​(JComponent component, Consumer<? super Collection<File>> action)
      Add support for dropping Files on a component.
      Parameters:
      component - the component to add drop support to
      action - the action to perform when files are dropped
    • addDropFilesSupport

      public static void addDropFilesSupport​(JComponent component, Consumer<? super Collection<File>> action, Predicate<? super Collection<File>> test, Consumer<? super Exception> exceptionHandler)
      Add support for dropping Files on a component.
      Parameters:
      component - the component to add drop support to
      action - the action to perform when files are dropped
      test - Predicate to decide whether dropping is allowed (should execute fast; called frequently during drag)
      exceptionHandler - handler to call when an exception is caught