Class TextFieldFileChooser

java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
com.everdro1d.libs.swing.components.TextFieldFileChooser
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable

public class TextFieldFileChooser extends JComponent
The TextFieldFileChooser class provides a text field with a button that opens a file chooser dialog.

This class is designed to be used in Swing applications and supports localization through the LocaleManager.

Features:

  • Text field for displaying the selected file path.
  • Button to open a file chooser dialog.
  • Customizable dialog title and behavior.

Example Usage:

 TextFieldFileChooser fileChooser = new TextFieldFileChooser(localeManager);
 fileChooser.setText("~/path/to/file.txt");
 

Or:

 TextFieldFileChooser fileChooser = new TextFieldFileChooser(localeManager) {
     @Override
     public void customActionOnApprove(File file) {
     // Custom action when a file is approved
     System.out.println("File selected: " + file.getAbsolutePath());
     }
 }
 
See Also:
  • Constructor Details

    • TextFieldFileChooser

      public TextFieldFileChooser(LocaleManager localeManager)
      Constructor for creating a TextFieldFileChooser with default options.

      Default params set to:

       selectFiles = true
       selectDirectories = true
       defaultPath = null
       
      Parameters:
      localeManager - the LocaleManager for localization. can be null
    • TextFieldFileChooser

      public TextFieldFileChooser(LocaleManager localeManager, String defaultPath)
      Constructor for creating a TextFieldFileChooser with customizable options.

      Default params set to:

       selectFiles = true
       selectDirectories = true
       
      Parameters:
      localeManager - the LocaleManager for localization. can be null
      defaultPath - the default path to display in the text field
    • TextFieldFileChooser

      public TextFieldFileChooser(LocaleManager localeManager, boolean selectFiles)
      Constructor for creating a TextFieldFileChooser with customizable options.

      Default params set to:

       selectDirectories = false
       defaultPath = null
       
      Parameters:
      localeManager - the LocaleManager for localization. can be null
      selectFiles - whether to allow file selection
    • TextFieldFileChooser

      public TextFieldFileChooser(LocaleManager localeManager, boolean selectFiles, String defaultPath)
      Constructor for creating a TextFieldFileChooser with customizable options.

      Default params set to:

       selectDirectories = false
       
      Parameters:
      localeManager - the LocaleManager for localization. can be null
      selectFiles - whether to allow file selection
      defaultPath - the default path to display in the text field
    • TextFieldFileChooser

      public TextFieldFileChooser(LocaleManager localeManager, boolean selectFiles, boolean selectDirectories)
      Constructor for creating a TextFieldFileChooser with customizable options.

      Default params set to:

       defaultPath = null
       
      Parameters:
      localeManager - the LocaleManager for localization. can be null
      selectFiles - whether to allow file selection
      selectDirectories - whether to allow directory selection
    • TextFieldFileChooser

      public TextFieldFileChooser(LocaleManager localeManager, boolean selectFiles, boolean selectDirectories, String defaultPath)
      Constructor for creating a TextFieldFileChooser with customizable options.
      Parameters:
      localeManager - the LocaleManager for localization. can be null
      selectFiles - whether to allow file selection
      selectDirectories - whether to allow directory selection
      defaultPath - the default path to display in the text field
  • Method Details

    • customActionOnApprove

      public void customActionOnApprove(File file)
      Override this method to perform custom actions when a file is approved. For example, you can open the file or perform some other action
      Parameters:
      file - the file that was approved
    • getTextField

      public JTextField getTextField()
      Gets the text field component.
      Returns:
      the JTextField used in this component
    • getButton

      public JButton getButton()
      Gets the button component.
      Returns:
      the JButton used in this component
    • getFileChooser

      public FileChooser getFileChooser()
      Gets the file chooser instance.
      Returns:
      the FileChooser instance used in this component
    • getText

      public String getText()
      Gets the text currently displayed in the text field.
      Returns:
      the text in the text field
    • setText

      public void setText(String text)
      Sets the text to be displayed in the text field.
      Parameters:
      text - the text to set in the text field
    • setEnabled

      public void setEnabled(boolean enabled)
      Enables or disables the text field and button.
      Overrides:
      setEnabled in class JComponent
      Parameters:
      enabled - true to enable, false to disable
    • setEditable

      public void setEditable(boolean editable)
      Sets whether the text field is editable.
      Parameters:
      editable - true to make the text field editable, false otherwise
    • setToolTipText

      public void setToolTipText(String text)
      Sets the tooltip text for both the text field and button.
      Overrides:
      setToolTipText in class JComponent
      Parameters:
      text - the tooltip text to set
    • setFont

      public void setFont(Font font)
      Sets the font for both the text field and button.
      Overrides:
      setFont in class JComponent
      Parameters:
      font - the Font to set
    • getFont

      public Font getFont()
      Gets the font used by the text field.
      Specified by:
      getFont in interface MenuContainer
      Overrides:
      getFont in class Component
      Returns:
      the Font of the text field
    • setSize

      public void setSize(Dimension size, int percentageButtonWidth, BiConsumer<JComponent,Dimension> setSizeFunc)
      Sets the size of the component, dividing the width between the text field and button based on the specified percentage for the button width.
      Parameters:
      size - the total size of the component
      percentageButtonWidth - the percentage of the total width allocated to the button
      setSizeFunc - a function to apply the size to the components
    • setSize

      public void setSize(Dimension size)
      Sets the preferred size of the component.
      Overrides:
      setSize in class Component
      Parameters:
      size - the preferred size of the component
    • setPreferredSize

      public void setPreferredSize(Dimension preferredSize)
      Sets the preferred size of the component.
      Overrides:
      setPreferredSize in class JComponent
      Parameters:
      preferredSize - the preferred size of the component
    • setPreferredSize

      public void setPreferredSize(Dimension preferredSize, int percentageButtonWidth)
      Sets the preferred size of the component, dividing the width between the text field and button based on the specified percentage for the button width.
      Parameters:
      preferredSize - the preferred size of the component
      percentageButtonWidth - the percentage of the total width allocated to the button
    • setMinimumSize

      public void setMinimumSize(Dimension minimumSize)
      Sets the minimum size of the component, allocating 10% of the width to the button by default.
      Overrides:
      setMinimumSize in class JComponent
      Parameters:
      minimumSize - the minimum size of the component
    • setMinimumSize

      public void setMinimumSize(Dimension minimumSize, int percentageButtonWidth)
      Sets the minimum size of the component, dividing the width between the text field and button based on the specified percentage for the button width.
      Parameters:
      minimumSize - the minimum size of the component
      percentageButtonWidth - the percentage of the total width allocated to the button
    • setMaximumSize

      public void setMaximumSize(Dimension maximumSize)
      Sets the maximum size of the component, allocating 10% of the width to the button by default.
      Overrides:
      setMaximumSize in class JComponent
      Parameters:
      maximumSize - the maximum size of the component
    • setMaximumSize

      public void setMaximumSize(Dimension maximumSize, int percentageButtonWidth)
      Sets the maximum size of the component, dividing the width between the text field and button based on the specified percentage for the button width.
      Parameters:
      maximumSize - the maximum size of the component
      percentageButtonWidth - the percentage of the total width allocated to the button