Package jahuwaldt.swing
Class AppUtilities
- java.lang.Object
-
- jahuwaldt.swing.AppUtilities
-
public class AppUtilities extends java.lang.Object
A set of generic utilities that I have found useful and that are used by most of my Java applications.Modified by: Joseph A. Huwaldt
- Version:
- December 12, 2023
- Author:
- Joseph A. Huwaldt, Date: February 16, 2000
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.io.File
addExtensionToFile(java.awt.Component parent, java.io.File theFile, java.lang.String extension, java.lang.String existsErrFmtMsg, java.lang.String dlgTitle)
Return a version of the provided file reference that has the specified extension on it.static javax.swing.ButtonGroup
buildButtonGroup(java.awt.image.ImageObserver parent, java.lang.String[][] defs, java.lang.String[] imgPaths)
Build up a ButtonGroup containing toggle buttons from a description stored in a String array.static javax.swing.ButtonGroup
buildButtonGroup(java.awt.image.ImageObserver parent, java.lang.String[][] defs, java.net.URL[] imgURLs)
Build up a ButtonGroup containing toggle buttons from a description stored in a String array.static javax.swing.ButtonGroup
buildButtonGroup(java.awt.image.ImageObserver parent, java.lang.String[][] defs, javax.swing.ImageIcon[] icons)
Build up a ButtonGroup containing toggle buttons from a description stored in a String array.static java.util.List<javax.swing.JButton>
buildButtonList(java.lang.Object parent, java.awt.image.ImageObserver observer, java.lang.String[][] defs)
Build up a List of buttons from a description stored in a String array.static void
buildConstraints(java.awt.GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy)
Fills in the GridBagConstraints record for a given component with input items.static javax.swing.JMenu
buildMenu(java.lang.Object eventTarget, java.lang.String name, java.lang.String[][] menuDesc)
Build up a JMenu from a description stored in a String array.static javax.swing.JMenu
buildMenu(java.lang.Object eventTarget, java.lang.String name, java.util.List<java.lang.String[]> menuDesc)
Build up a JMenu from a description stored in a list of String arrays.static javax.swing.JMenuItem
buildMenuItem(java.lang.Object eventTarget, java.lang.String[] menuDesc)
Build up a JMenuItem from a description stored in a String array.static java.awt.Point
centerIt(java.awt.Component comp)
Center the specified component on the screen.static java.awt.Point
centerIt(java.awt.Component outside, java.awt.Component inside)
Center the "inside" component inside of the bounding rectangle of the "outside" component.static void
checkDateAndDie(java.util.Calendar date, java.lang.String message)
Method that exits the program (with a warning message) if the current data is after the specified date.static java.awt.Point
dialogPosition(java.awt.Component comp)
Returns a point that can be used to locate a component in the "dialog" position (1/3 of the way from the top to the bottom of the screen).static java.awt.event.ActionListener
getActionListenerForMethod(java.lang.Object target, java.lang.String methodStr)
Method that returns an action listener that simply calls the specified method in the specified class.static java.net.URL
getDocumentBase()
Try and determine the directory to where the program is installed and return that as a URL.static java.awt.Frame
getFrameForComponent(java.awt.Component parentComponent)
Returns the specified component's top-levelFrame
.static java.awt.Cursor
getImageCursor(java.lang.String path, int hsx, int hsy, java.lang.String name, java.awt.image.ImageObserver observer)
Method that loads an image from a file and creates a custom mouse cursor from it.static java.awt.Cursor
getImageCursor(java.net.URL url, int hsx, int hsy, java.lang.String name, java.awt.image.ImageObserver observer)
Method that loads an image from a URL and creates a custom mouse cursor from it.static java.awt.Component
getRootComponent(java.lang.Object source)
Return the root component of the given component.static boolean
isMacOS()
Returns true if this program is running in any MacOS environment, false is returned otherwise.static boolean
isSystemLAF()
Returns true if the current look and feel is the same as the system look and feel.static boolean
isWindows()
Returns true if this program is running in a MS Windows environment, false is returned otherwise.static java.awt.Cursor
makeImageCursor(java.awt.Image img, int hsx, int hsy, java.lang.String name, java.awt.image.ImageObserver observer)
Method that creates a custom mouse cursor from the specified image.static void
positionWindow(java.awt.Window inWindow, int width, int height)
Positions the specified window neatly on the screen such that document windows are staggered one after the other.static void
positionWindowFixedSize(java.awt.Window inWindow, int width, int height)
Positions the specified window neatly on the screen such that document windows are staggered one after the other.static java.io.File
selectDirectory(java.awt.Component parent, int mode, java.lang.String message, java.lang.String directory, java.io.FilenameFilter filter)
Method that brings up a file chooser dialog and allows the user to select a directory.static java.io.File
selectFile(java.awt.Component parent, int mode, java.lang.String message, java.lang.String directory, java.lang.String name, java.io.FilenameFilter filter)
Method that brings up a file chooser dialog and allows the user to select a file.static java.io.File
selectFile4Save(java.awt.Component parent, java.lang.String message, java.lang.String directory, java.lang.String name, java.io.FilenameFilter filter, java.lang.String extension, java.lang.String existsErrFmtMsg, java.lang.String dlgTitle)
Method that displays a "Save As..." dialog asking the user to select or input a file name to save a file to and returns a reference to the chosen file.static void
setSystemLAF()
Sets the Swing look and feel to hide that hideous default Java LAF.static void
showException(java.awt.Component parent, java.lang.String title, java.lang.String message, java.lang.Throwable th)
Method that displays a dialog with a scrollable text field that contains the text of a Java exception message.static java.lang.Integer
userEnteredInteger(java.awt.Component parent, java.lang.String msg, java.lang.String title, int messageType, int initialValue, int lowerBound, int upperBound)
Method that returns the bounded integer value that a user has entered in a standard input dialog.static int
userSelectionFromArray(java.awt.Component parent, java.lang.Object msg, java.lang.String title, int messageType, java.lang.Object[] selectionValues, java.lang.Object initialSelectionValue)
Method that returns the index to an option that a user has selected from an array of options using a standard input dialog.
-
-
-
Method Detail
-
isMacOS
public static boolean isMacOS()
Returns true if this program is running in any MacOS environment, false is returned otherwise.- Returns:
- true if this program is running in any MacOS environment, false is returned otherwise.
-
isWindows
public static boolean isWindows()
Returns true if this program is running in a MS Windows environment, false is returned otherwise.- Returns:
- true if this program is running in a MS Windows environment.
-
showException
public static void showException(java.awt.Component parent, java.lang.String title, java.lang.String message, java.lang.Throwable th)
Method that displays a dialog with a scrollable text field that contains the text of a Java exception message. This allows caught exceptions to be displayed in a GUI rather than being hidden at a console window or not displayed at all.- Parameters:
parent
- The component that the exception dialog should be associated with (null
is fine).title
- The title of the dialog window.message
- An optional message to display above the text pane (null
is fine).th
- The exception to be displayed in the text pane of the dialog.
-
centerIt
public static java.awt.Point centerIt(java.awt.Component outside, java.awt.Component inside)
Center the "inside" component inside of the bounding rectangle of the "outside" component.- Parameters:
outside
- The component that the "inside" component should be center on.inside
- The component that is to be center on the "outside" component.- Returns:
- A point representing the upper left corner location required to center the inside component in the outside one.
-
centerIt
public static java.awt.Point centerIt(java.awt.Component comp)
Center the specified component on the screen.- Parameters:
comp
- The component to be centered on the display screen.- Returns:
- A point representing the upper left corner location required to center the specified component on the screen.
-
dialogPosition
public static java.awt.Point dialogPosition(java.awt.Component comp)
Returns a point that can be used to locate a component in the "dialog" position (1/3 of the way from the top to the bottom of the screen).- Parameters:
comp
- The component to be located in a "dialog" position.- Returns:
- A point representing the upper left corner location required to locate a component in the "dialog" position on the screen.
-
positionWindow
public static void positionWindow(java.awt.Window inWindow, int width, int height)
Positions the specified window neatly on the screen such that document windows are staggered one after the other. The window is set to the input width and height. Care is also taken to make sure the window will fit on the screen.- Parameters:
inWindow
- The window to be positioned neatly on the screen.width
- the width to set the window to.height
- the height to set the window to.
-
positionWindowFixedSize
public static void positionWindowFixedSize(java.awt.Window inWindow, int width, int height)
Positions the specified window neatly on the screen such that document windows are staggered one after the other. The size of the window is also set to the input values. This version assumes that windows are a fixed size and can not be shrunk.- Parameters:
inWindow
- The window to be placed neatly on the screen.width
- the width to set the window to.height
- the height to set the window to.
-
buildConstraints
public static void buildConstraints(java.awt.GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy)
Fills in the GridBagConstraints record for a given component with input items.- Parameters:
gbc
- The GridBagConstraints record to be filled in.gx
- Grid (cell) index for this component in the x direction (1 == 2nd column).gy
- Grid (cell) index for this component in the y direction (3 == 4th row).gw
- The number of cells this component spans in width.gh
- The number of cells this component spans in height.wx
- Proportional width of this grid cell compared to others.wy
- Proportional height of this grid cell compared to others.
-
getDocumentBase
public static java.net.URL getDocumentBase()
Try and determine the directory to where the program is installed and return that as a URL. Has essentially the same function as Applet.getDocumentBase(), but works on local file systems in applications.- Returns:
- A URL pointing to the directory where the program is installed.
-
getRootComponent
public static java.awt.Component getRootComponent(java.lang.Object source)
Return the root component of the given component.- Parameters:
source
- The Component or MenuComponent to find the root Component for.- Returns:
- the root component of the given component.
-
getFrameForComponent
public static java.awt.Frame getFrameForComponent(java.awt.Component parentComponent)
Returns the specified component's top-levelFrame
.- Parameters:
parentComponent
- theComponent
to check for aFrame
- Returns:
- the
Frame
that contains the component, ornull
if the component isnull
, or does not have a validFrame
parent
-
getImageCursor
public static java.awt.Cursor getImageCursor(java.net.URL url, int hsx, int hsy, java.lang.String name, java.awt.image.ImageObserver observer)
Method that loads an image from a URL and creates a custom mouse cursor from it.- Parameters:
url
- A URL to the image to be loaded as a cursor.hsx
- The x-coordinate of the point on the image that represents the cursor hot spot.hsy
- The y coordinate of the point on the image that represents the cursor hot spot.name
- The name to assign to this cursor for Java Accessibility.observer
- The component to use to observe the loading of the image. Passnull
if none.- Returns:
- The custom cursor generated from the specified image. If any error occurs
null
will be returned.
-
getImageCursor
public static java.awt.Cursor getImageCursor(java.lang.String path, int hsx, int hsy, java.lang.String name, java.awt.image.ImageObserver observer)
Method that loads an image from a file and creates a custom mouse cursor from it.- Parameters:
path
- The path to the image to be loaded as a cursor.hsx
- The x-coordinate of the point on the image that represents the cursor hot spot.hsy
- The y coordinate of the point on the image that represents the cursor hot spot.name
- The name to assign to this cursor for Java Accessibility.observer
- The component to use to observe the loading of the image. Passnull
if none.- Returns:
- The custom cursor generated from the specified image. If any error occurs
null
will be returned.
-
makeImageCursor
public static java.awt.Cursor makeImageCursor(java.awt.Image img, int hsx, int hsy, java.lang.String name, java.awt.image.ImageObserver observer)
Method that creates a custom mouse cursor from the specified image.- Parameters:
img
- The image to be loaded as a cursor.hsx
- The x-coordinate of the point on the image that represents the cursor hot spot.hsy
- The y coordinate of the point on the image that represents the cursor hot spot.name
- The name to assign to this cursor for Java Accessibility.observer
- The component to use to observe the loading of the image. Passnull
if none.- Returns:
- The custom cursor generated from the specified image. If any error occurs
null
will be returned.
-
setSystemLAF
public static void setSystemLAF() throws javax.swing.UnsupportedLookAndFeelException, java.lang.IllegalAccessException, java.lang.ClassNotFoundException, java.lang.InstantiationException
Sets the Swing look and feel to hide that hideous default Java LAF.- Throws:
javax.swing.UnsupportedLookAndFeelException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.lang.InstantiationException
-
isSystemLAF
public static boolean isSystemLAF()
Returns true if the current look and feel is the same as the system look and feel. Otherwise, returns false.- Returns:
- true if the current look and feel is the system look and feel.
-
buildMenu
public static javax.swing.JMenu buildMenu(java.lang.Object eventTarget, java.lang.String name, java.util.List<java.lang.String[]> menuDesc) throws java.lang.NoSuchMethodException
Build up a JMenu from a description stored in a list of String arrays. The description contains the text of each menu item, accelerator key, and the name of a public method in the specified "eventTarget" object that handles the menu item action event.- Parameters:
eventTarget
- This is the object that must contain the methods used to handle action events for this menu's items.name
- Name of the menu to create.menuDesc
- List of String arrays that describes each of the menu items that will be built into this menu. For each String array, item 0 is the menu item text string. Item 1 is the accelerator key for the menu item. Item 2 is the name of the public method in "eventTarget" that will handle the user choosing the menu item. This method must take an ActionEvent as it's only parameter. If this isnull
, the menu item will be shown as disabled. Items 3 is optional and is the tool-tip text to show for the menu item if notnull
.- Returns:
- A menu built up from the menu description supplied.
- Throws:
java.lang.NoSuchMethodException
- If one of the action event methods in parent could not be found.
-
buildMenu
public static javax.swing.JMenu buildMenu(java.lang.Object eventTarget, java.lang.String name, java.lang.String[][] menuDesc) throws java.lang.NoSuchMethodException
Build up a JMenu from a description stored in a String array. The description contains the text of each menu item, accelerator key, and the name of a public method in the specified "eventTarget" object that handles the menu item action event.- Parameters:
eventTarget
- This is the object that must contain the public methods used to handle action events for this menu's items.name
- Name of the menu to create.menuDesc
- String array that describes each of the menu items that will be built into this menu. For each String array, item 0 is the menu item text string. Item 1 is the accelerator key for the menu item. Item 2 is the name of the public method in "eventTarget" that will handle the user choosing the menu item. This method must take an ActionEvent as it's only parameter. If this isnull
, the menu item will be shown as disabled. Items 3 is optional and is the tool-tip text to show for the menu item if notnull
.- Returns:
- A menu built up from the menu description supplied.
- Throws:
java.lang.NoSuchMethodException
- If one of the action event methods in parent could not be found.
-
buildMenuItem
public static javax.swing.JMenuItem buildMenuItem(java.lang.Object eventTarget, java.lang.String[] menuDesc) throws java.lang.NoSuchMethodException
Build up a JMenuItem from a description stored in a String array. The description contains the text of each menu item, accelerator key, and the name of a public method in the specified "eventTarget" object that handles the menu item action event.- Parameters:
eventTarget
- This is the object that must contain the public methods used to handle action events for this item.menuDesc
- A 3 or 4-element String array that describes the menu item that will be built. Item 0 is the menu item text string. Item 1 is the accelerator key for the menu item. Item 2 is the name of the public method in "eventTarget" that will handle the user choosing this menu item. This method must take an ActionEvent as it's only parameter. If this isnull
, the menu item will be shown as disabled. Items 3 is optional and is the tool-tip text to show for the menu item if notnull
.- Returns:
- A new JMenuItem built from the description in menuDesc with action listeners that call methods in parent.
- Throws:
java.lang.NoSuchMethodException
- If one of the action event methods in parent could not be found.
-
buildButtonGroup
public static javax.swing.ButtonGroup buildButtonGroup(java.awt.image.ImageObserver parent, java.lang.String[][] defs, java.net.URL[] imgURLs) throws java.lang.NoSuchMethodException
Build up a ButtonGroup containing toggle buttons from a description stored in a String array. The description contains, for each button, the text to appear in the button (null
if no text), the name of the method in the parent object that will be called when a button is clicked on (ifnull
is passed, the button is disabled), and the tool tip text to display for the button; (null
for no tool tip).- Parameters:
parent
- The parent object that will contain handle the user clicking on one of the buttons in this group. This object is also used to observe the reading in of the button icon images.defs
- String array that describes each of the text string that will be displayed in the button. Item 0 is the text to be displayed in each button. If this isnull
no text is displayed. Item 1 is the name of the method in "parent" that will handle the user clicking on this button item. If this isnull
, the menu button will be shown as disabled. Item 2 is the tool tip to be shown above this button item. Ifnull
is passed no tool tip is shown.imgURLs
- An array of URLs for the images to be displayed in the buttons. Ifnull
is passed in, then there will be no button images. If any element isnull
, that button will have no image.- Returns:
- A button group built up from the button description supplied.
- Throws:
java.lang.NoSuchMethodException
- if a specified method in the parent object doesn't exist.
-
buildButtonGroup
public static javax.swing.ButtonGroup buildButtonGroup(java.awt.image.ImageObserver parent, java.lang.String[][] defs, java.lang.String[] imgPaths) throws java.lang.NoSuchMethodException
Build up a ButtonGroup containing toggle buttons from a description stored in a String array. The description contains, for each button, the text to appear in the button (null
if no text), the name of the method in the parent object that will be called when a button is clicked on (ifnull
is passed, the button is disabled), and the tool tip text to display for the button (null
for no tool tip).- Parameters:
parent
- The parent object that will contain handle the user clicking on one of the buttons in this group. This object is also used to observe the reading in of the button icon images.defs
- String array that describes each of the text string that will be displayed in the button. Item 0 is the text to be displayed in each button. If this isnull
no text is displayed. Item 1 is the name of the method in "parent" that will handle the user clicking on this button item. If this isnull
, the menu button will be shown as disabled. Item 2 is the tool tip to be shown above this button item. Ifnull
is passed no tool tip is shown.imgPaths
- An array of image file paths for the images to be displayed in the buttons. Ifnull
is passed in, then there will be no button images. If any element isnull
, that button will have no image.- Returns:
- A button group built up from the button description supplied.
- Throws:
java.lang.NoSuchMethodException
- if a specified method in the parent object doesn't exist.
-
buildButtonGroup
public static javax.swing.ButtonGroup buildButtonGroup(java.awt.image.ImageObserver parent, java.lang.String[][] defs, javax.swing.ImageIcon[] icons) throws java.lang.NoSuchMethodException
Build up a ButtonGroup containing toggle buttons from a description stored in a String array. The description contains, for each button, the text to appear in the button (null
if no text), the path to an image file to display in the button (null
for no image), the name of the method in the parent object that will be called when a button is clicked on (ifnull
is passed, the button is disabled), and the tool tip text to display for the button (null
for no tool tip).- Parameters:
parent
- The parent object that will contain handle the user clicking on one of the buttons in this group. This object is also used to observe the reading in of the button icon images.defs
- String array that describes each of the text string that will be displayed in the button. Item 0 is the text to be displayed in each button. If this isnull
no text is displayed. Item 1 is the name of the method in "parent" that will handle the user clicking on this button item. If this isnull
, the menu button will be shown as disabled. Item 2 is the tool tip to be shown above this button item. Ifnull
is passed no tool tip is shown.icons
- An array of icon images to be displayed in the buttons. If this array isnull
, there will be no images displayed in the buttons. If one of the elements of the array isnull
, that button will have no image.- Returns:
- A button group built up from the button description supplied.
- Throws:
java.lang.NoSuchMethodException
- if a specified method in the parent object doesn't exist.
-
buildButtonList
public static java.util.List<javax.swing.JButton> buildButtonList(java.lang.Object parent, java.awt.image.ImageObserver observer, java.lang.String[][] defs) throws java.lang.NoSuchMethodException
Build up a List of buttons from a description stored in a String array. The description contains, for each button, the text to appear in the button (null
if no text), the path to an image file to display in the button (null
for no image), the name of the method in the parent object that will be called when a button is clicked on (ifnull
is passed, the button is disabled), and the tool tip text to display for the button (null
for no tool tip).- Parameters:
parent
- The parent object that will handle the user clicking on one of the buttons in this group.observer
- The object used to observe the reading in of the button icon images, if any (if no images being read in, this may be null).defs
- String array that describes each button. Item 0 is the text string that will be displayed in the button. If this isnull
no text is displayed. Item 1 is the file path to the image to display in this button. Ifnull
is passed, no image icon is displayed. Item 2 is the name of the method in "parent" that will handle the user clicking on this button item. If this isnull
, the menu button will be shown as disabled. Item 3 is the tool tip to be shown above this button item. Ifnull
is passed no tool tip is shown.- Returns:
- A List containing all the buttons built up from the description supplied.
- Throws:
java.lang.NoSuchMethodException
- if a specified method in the parent object doesn't exist.
-
getActionListenerForMethod
public static java.awt.event.ActionListener getActionListenerForMethod(java.lang.Object target, java.lang.String methodStr) throws java.lang.NoSuchMethodException
Method that returns an action listener that simply calls the specified method in the specified class. This is little trick is done using the magic of reflection.- Parameters:
target
- The object which contains the specified method.methodStr
- The name of the method to be called in the target object. This method must be contained in target, must be publicly accessible and must accept an ActionEvent object as the only parameter.- Returns:
- An ActionListener that will call the specified method in the specified target object and pass to it an ActionEvent object.
- Throws:
java.lang.NoSuchMethodException
- if the target object does not contain the specified method.
-
checkDateAndDie
public static void checkDateAndDie(java.util.Calendar date, java.lang.String message)
Method that exits the program (with a warning message) if the current data is after the specified date.- Parameters:
date
- The data and time when the program should expire.message
- The warning message to show if the program has expired.
-
userSelectionFromArray
public static int userSelectionFromArray(java.awt.Component parent, java.lang.Object msg, java.lang.String title, int messageType, java.lang.Object[] selectionValues, java.lang.Object initialSelectionValue)
Method that returns the index to an option that a user has selected from an array of options using a standard input dialog.- Parameters:
parent
- The parent Component for the dialogmsg
- The message to display to the user.title
- The title of the dialog window.messageType
- One of the JOptionPane message type constants: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE.selectionValues
- An array of Objects that gives the possible selections.initialSelectionValue
- The value used to initialize the input field.- Returns:
- The index into the input array of the value the user has selected or -1 if the user canceled.
-
userEnteredInteger
public static java.lang.Integer userEnteredInteger(java.awt.Component parent, java.lang.String msg, java.lang.String title, int messageType, int initialValue, int lowerBound, int upperBound)
Method that returns the bounded integer value that a user has entered in a standard input dialog.- Parameters:
parent
- The parent Component for the dialogmsg
- The message to display to the user.title
- The title of the dialog window.messageType
- One of the JOptionPane message type constants: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE.initialValue
- The initial value for the dialog's text field.lowerBound
- The lowest (most negative) value to allow the user to enter (use Integer.MIN_VALUE to allow any negative integer).upperBound
- The largest (most positive) value to allow the user to enter (use Integer.MAX_VALUE to allow any positive integer).- Returns:
- The integer value the user has entered or
null
if the user has canceled.
-
selectFile
public static java.io.File selectFile(java.awt.Component parent, int mode, java.lang.String message, java.lang.String directory, java.lang.String name, java.io.FilenameFilter filter)
Method that brings up a file chooser dialog and allows the user to select a file.- Parameters:
parent
- The owner of the dialog (null
is fine).mode
- Either FileDialog.LOAD or FileDialog.SAVE.message
- The message for the dialog. Something like "Choose a name for this file:".directory
- The directory to prompt the user with by default (null
is fine).name
- The name of the file to prompt the user with by default (null
is fine).filter
- The filename filter to use (null
means no filter).- Returns:
- The file selected by the user, or
null
if no file was selected.
-
selectDirectory
public static java.io.File selectDirectory(java.awt.Component parent, int mode, java.lang.String message, java.lang.String directory, java.io.FilenameFilter filter)
Method that brings up a file chooser dialog and allows the user to select a directory.- Parameters:
parent
- The owner of the dialog (null
is fine).mode
- Either FileDialog.LOAD or FileDialog.SAVE.message
- The message for the dialog. Something like "Choose a name for this directory:".directory
- The directory to prompt the user with by default (null
is fine).filter
- The filename filter to use (null
means no filter).- Returns:
- The file selected by the user, or
null
if no file was selected.
-
selectFile4Save
public static java.io.File selectFile4Save(java.awt.Component parent, java.lang.String message, java.lang.String directory, java.lang.String name, java.io.FilenameFilter filter, java.lang.String extension, java.lang.String existsErrFmtMsg, java.lang.String dlgTitle)
Method that displays a "Save As..." dialog asking the user to select or input a file name to save a file to and returns a reference to the chosen file. This version of selectFile automatically adds a user supplied extension to the file returned if it doesn't already have it. N.B.: No test is made to see if the user can actually write to that file! See: File.canWrite().- Parameters:
parent
- The owner of the dialog (null
is fine).message
- The message for the dialog. Something like "Choose a name for this file:".directory
- The directory to prompt the user with by default (null
is fine).name
- The name of the file to prompt the user with by default (null
is fine).filter
- The filename filter to use (null
means no filter).extension
- The filename extension. This is appended to the filename provided by the user if the filename input doesn't already have it. Passingnull
means that no extension will be forced onto the filename.existsErrFmtMsg
- A MessageFormat compatible "file exists" error message that will have the file name substituted into it. Pass something like, "A file with the named \"{0}\" already exists. Do you want to replace it?"dlgTitle
- The title of the warning dialog that is shown if the selected file already exists.- Returns:
- The file chosen by the user for saving out a file. Returns
null
if the a valid file was not chosen.
-
addExtensionToFile
public static java.io.File addExtensionToFile(java.awt.Component parent, java.io.File theFile, java.lang.String extension, java.lang.String existsErrFmtMsg, java.lang.String dlgTitle)
Return a version of the provided file reference that has the specified extension on it. This is intended to be used as part of processing a user input file name.- If the input file already has the extension, it is simply returned.
- If the input file doesn't have the extension, it is added to the end.
- If the modified file reference is an existing file, the user is asked if they want to overwrite it.
- If the user chooses to overwrite the existing file, then the file reference is returned.
- If the user chooses not to overwrite it, then
null
is returned.
- Parameters:
parent
- The owner of the dialog (null
is fine).theFile
- The file to enforce an extension for. Ifnull
, then this dialog does nothing.extension
- The extension to ensure the file has. Ifnull
, then this method does nothing.existsErrFmtMsg
- A MessageFormat compatible "file exists" error message that will have the file name substituted into it. Pass something like, "A file with the named \"{0}\" already exists. Do you want to replace it?"dlgTitle
- The title of the warning dialog that is shown if the selected file already exists.- Returns:
- a version of the provided file reference that has the specified extension on it.
-
-