Class JPanelFileSelection

All Implemented Interfaces:
ActionListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible

public class JPanelFileSelection extends JPanelEdit implements ActionListener
provides a nice plug-in swing panel to simplify the file-selection process. Contains a textfield on the left and a browse button on the right to bring a JFileChooser front, either of which may be used to indicate the file. This object should work just fine for both "open" applications as well as "save-as" applications, and indeed, "folder-select" applications. The Panel may choose between permitting selection of folders and both folders and directories in its file chooser.

This panel has an etched, titled border.

Handles its own button clicks and text, and when the file it points to is requested. The preferred file is chosen based on the text in the textfield, as there is no way to ensure that a mouseExit event or focusExit event will occur before some kind of OKAY button ActionEvent which requests the file. SO.. the file is translated from the text when the file is requested. If it can't find the chosen file from the text (ie., the path does not exist, or the text was left blank), then the file comes from the button choice, which should be otherwise no different from the textfield. Be aware that if a file does not exist in the textfield, a file of that name can still be returned -- this Panel is designed to be perfectly fine with save-as type of applications.

The file that comes back is unadulterated and not made. Possible future expansion may be for specialising this for File input, or output, ensuring that the file exists, or finding a way to set up this panel to fire notification when a file is chosen (using either the text or the file chooser).

See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      Use serialVersionUID for interoperability.
      See Also:
    • defaultDirectory

      public static String defaultDirectory
      Specifies the default directory for the panel editor, which is the "user.home" directory, with a separator character appended at the end.
    • chooser

      private JFileChooser chooser
      DOCUMENT ME!
    • chosenFile

      private File chosenFile
      DOCUMENT ME!
    • fileBrowse

      private JButton fileBrowse
      DOCUMENT ME!
    • fileTextField

      private JTextField fileTextField
      DOCUMENT ME!
  • Constructor Details

    • JPanelFileSelection

      public JPanelFileSelection()
      basic constructor, creates a panel with textfield and browser. defaults to pointing at the user's default directory, There is no title for either the file chooser or the panel itself, and the file chooser will search for both files and directories.
    • JPanelFileSelection

      public JPanelFileSelection(boolean browseFiles)
      custom constructor -- selects the preference for the file chooser to permit choice of selection of directories only or both files and directories. Uses the default user - directory location in the text field, and will have no title on the border.
      Parameters:
      browseFiles - DOCUMENT ME!
    • JPanelFileSelection

      public JPanelFileSelection(File prechosenFile, String title)
      custom constructor -- Selection of the starting directory, and the title on the border. Chooser selects both files and directories.
      Parameters:
      prechosenFile - DOCUMENT ME!
      title - DOCUMENT ME!
    • JPanelFileSelection

      public JPanelFileSelection(File prechosenFile, String title, boolean browseFiles)
      custom constructor -- Selection of the starting directory, and the title on the border. Chooser selects based on argument.
      Parameters:
      prechosenFile - DOCUMENT ME!
      title - DOCUMENT ME!
      browseFiles - DOCUMENT ME!
  • Method Details

    • actionPerformed

      public void actionPerformed(ActionEvent action)
      Responds to ActionEvents, such as the "Browse" button being clicked. When the Browse button is selected, if it returns a file, it will provide the path of the chosen file to the text field. It then fires a property change for all interested listeners.
      Specified by:
      actionPerformed in interface ActionListener
      Parameters:
      action - DOCUMENT ME!
    • checkFields

      public boolean checkFields()
      DOCUMENT ME!
      Specified by:
      checkFields in class JPanelEdit
      Returns:
      DOCUMENT ME!
    • getCodedValue

      public Object getCodedValue()
      DOCUMENT ME!
      Specified by:
      getCodedValue in class JPanelEdit
      Returns:
      DOCUMENT ME!
    • getPanelValue

      public String getPanelValue()
      DOCUMENT ME!
      Specified by:
      getPanelValue in class JPanelEdit
      Returns:
      DOCUMENT ME!
    • getSelectedFile

      public File getSelectedFile()
      we are enforcing existance at the calling/owning object level this panel doens't care if the file exists, nor if there are read rights or write rights there. returns the File pointed to by the text field, unless it is empty; at that point, we assume the file chooser was used to point to a file and the chosen file is stored there.
      Returns:
      DOCUMENT ME!
    • highlight

      public void highlight() throws NullPointerException
      gets the focus on the textfield and selects all text there. Recommended for error highlighting.
      Throws:
      NullPointerException - DOCUMENT ME!
    • setBrowseForFiles

      public void setBrowseForFiles(boolean browseFiles)
      determines how the file chooser will search. true will set the file chooser to permit viewing both File and directories, where-as false will only let the user select directories.
      Parameters:
      browseFiles - DOCUMENT ME!
    • setBrowserFileFilter

      public void setBrowserFileFilter(FileFilter filter)
      sets the file filter for the file chooser.
      Parameters:
      filter - DOCUMENT ME!
    • setBrowserTitle

      public void setBrowserTitle(String title)
      Sets the file chooser title independantly of the panel title.
      Parameters:
      title - DOCUMENT ME!
    • setCurrentDirectory

      public void setCurrentDirectory(File current)
      sets the file chooser and the text field to point to a different file. See JFileChooser.setCurrentDirectory for the resetting process if an invalid File is provided.
      Parameters:
      current - DOCUMENT ME!
    • setPanelTitle

      public void setPanelTitle(String title)
      Sets the panel title independant of the file chooser title.
      See Also:
      • invalid reference
        #setBrowserTitle()