java.lang.Object
com.dua3.utility.swing.SwingUtil
Utility methods for Swing applications.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidaddDropFilesSupport(JComponent component, Consumer<? super Collection<File>> action)Add support for dropping Files on a component.static voidaddDropFilesSupport(JComponent component, Consumer<? super Collection<File>> action, Predicate<? super Collection<File>> test, Consumer<? super Exception> exceptionHandler)Add support for dropping Files on a component.static ActioncreateAction(String name, Runnable onActionPerformed)Create an action to be used in menus.static ActioncreateAction(String name, Consumer<? super ActionEvent> onActionPerformed)Create an action to be used in menus.static JScrollPanecreateJScrollPane(int unitIncrement)Create JScrollPane with unit increment set.static JScrollPanecreateJScrollPane(int unitIncrement, int vsbPolicy, int hsbPolicy)Create JScrollPane with unit increment set.static JScrollPanecreateJScrollPane(int unitIncrement, Component view)Create JScrollPane with unit increment set.static JScrollPanecreateJScrollPane(int unitIncrement, Component view, int vsbPolicy, int hsbPolicy)Create JScrollPane with unit increment set.static voidScroll pane to bottom.static voidscrollToEnd(JScrollBar sb)Scroll scrollbar to end.static voidsetClipboardText(String text)Set clipboard content.static voidSet the Swing Look&Feel to the native Look&Feel.static voidsetNativeLookAndFeel(String applicationName)Set the Swing Look&Feel to the native Look&Feel.static voidsetUnitIncrement(JScrollBar sb, int unitIncrement)Set the unit increment.static voidsetUnitIncrement(JScrollPane jsp, int unitIncrement)Set the unit increment.showDirectoryOpenDialog(Component parent, Path current)Show directory open dialog.showFileOpenDialog(Component parent, Path current, com.dua3.utility.data.Pair<String,String[]>... types)Show file open dialog.showFileSaveDialog(Component parent, Path current, com.dua3.utility.data.Pair<String,String[]>... types)Show file save dialog.showOpenDialog(Component parent, Path current, int selectionMode, com.dua3.utility.data.Pair<String,String[]>... types)Show open dialog.static ColortoAwtColor(com.dua3.utility.data.Color color)Convert Color to java.awt.Color.static ColortoAwtColor(String s)Convert String to java.awt.Color.static com.dua3.utility.data.ColorConvert java.awt.Color to Color.static voidupdateAndScrollToBottom(JScrollPane sp, Runnable update)Execute update and scroll to bottom.static voidupdateAndScrollToEnd(JScrollBar sb, Runnable update)Execute update and scroll to end.
-
Method Details
-
createAction
Create an action to be used in menus.- Parameters:
name- the name to displayonActionPerformed- the Consumer that gets called when the Action is invoked- Returns:
- new Action instance
-
createAction
Create an action to be used in menus.- Parameters:
name- the name to displayonActionPerformed- the Runnable that gets called when the Action is invoked- Returns:
- new Action instance
-
scrollToBottom
Scroll pane to bottom.- Parameters:
sp- a scroll pane
-
scrollToEnd
Scroll scrollbar to end.- Parameters:
sb- a scroll bar
-
setClipboardText
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
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
Show directory open dialog.- Parameters:
parent- the parent component for the dialogcurrent- 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 dialogcurrent- the current file, it determines the folder shown when the dialog openstypes- 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 dialogcurrent- the current file, it determines the folder shown when the dialog openstypes- 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
Convert java.awt.Color to Color.- Parameters:
color- java.awt.Color to be converted- Returns:
- Color
-
toAwtColor
Convert Color to java.awt.Color.- Parameters:
color- Color to be converted- Returns:
- java.awt.Color
-
toAwtColor
Convert String to java.awt.Color.- Parameters:
s- String to be converted- Returns:
- java.awt.Color
- See Also:
Color.valueOf(String),toAwtColor(Color)
-
updateAndScrollToBottom
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 paneupdate- Runnable to do the update
-
updateAndScrollToEnd
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 barupdate- 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 dialogcurrent- the current path selected when the dialohg opensselectionMode- the selection mode as used inJFileChooser.setFileSelectionMode(int)types- pairs(desription, list of extensions) used to createFileNameExtensionFilterinstances- Returns:
- an Optional holding the selected path or an empty Optional if nothing was selected
-
setUnitIncrement
Set the unit increment.- Parameters:
sb- the scroll bar,nullis allowedunitIncrement- the unit increment
-
setUnitIncrement
Set the unit increment.- Parameters:
jsp- the scroll paneunitIncrement- the unit increment
-
createJScrollPane
Create JScrollPane with unit increment set.- Parameters:
unitIncrement- the unit increment- Returns:
- new JScrollPane
-
createJScrollPane
Create JScrollPane with unit increment set.- Parameters:
unitIncrement- the unit incrementview- the view- Returns:
- new JScrollPane
-
createJScrollPane
Create JScrollPane with unit increment set.- Parameters:
unitIncrement- the unit incrementvsbPolicy- vertical scroll bar policyhsbPolicy- 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 incrementview- the viewvsbPolicy- vertical scroll bar policyhsbPolicy- 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 toaction- 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 toaction- the action to perform when files are droppedtest- Predicate to decide whether dropping is allowed (should execute fast; called frequently during drag)exceptionHandler- handler to call when an exception is caught
-