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
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:
-
Nested Class Summary
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Fields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
Fields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Constructor Summary
ConstructorsConstructorDescriptionTextFieldFileChooser
(LocaleManager localeManager) Constructor for creating a TextFieldFileChooser with default options.TextFieldFileChooser
(LocaleManager localeManager, boolean selectFiles) Constructor for creating a TextFieldFileChooser with customizable options.TextFieldFileChooser
(LocaleManager localeManager, boolean selectFiles, boolean selectDirectories) Constructor for creating a TextFieldFileChooser with customizable options.TextFieldFileChooser
(LocaleManager localeManager, boolean selectFiles, boolean selectDirectories, String defaultPath) Constructor for creating a TextFieldFileChooser with customizable options.TextFieldFileChooser
(LocaleManager localeManager, boolean selectFiles, String defaultPath) Constructor for creating a TextFieldFileChooser with customizable options.TextFieldFileChooser
(LocaleManager localeManager, String defaultPath) Constructor for creating a TextFieldFileChooser with customizable options. -
Method Summary
Modifier and TypeMethodDescriptionvoid
customActionOnApprove
(File file) Override this method to perform custom actions when a file is approved.Gets the button component.Gets the file chooser instance.getFont()
Gets the font used by the text field.getText()
Gets the text currently displayed in the text field.Gets the text field component.void
setEditable
(boolean editable) Sets whether the text field is editable.void
setEnabled
(boolean enabled) Enables or disables the text field and button.void
Sets the font for both the text field and button.void
setMaximumSize
(Dimension maximumSize) Sets the maximum size of the component, allocating 10% of the width to the button by default.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.void
setMinimumSize
(Dimension minimumSize) Sets the minimum size of the component, allocating 10% of the width to the button by default.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.void
setPreferredSize
(Dimension preferredSize) Sets the preferred size of the component.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.void
Sets the preferred size of the component.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.void
Sets the text to be displayed in the text field.void
setToolTipText
(String text) Sets the tooltip text for both the text field and button.Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getPreferredSize, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUI, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAccessibleContext, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Constructor Details
-
TextFieldFileChooser
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
Constructor for creating a TextFieldFileChooser with customizable options.Default params set to:
selectFiles = true selectDirectories = true
- Parameters:
localeManager
- the LocaleManager for localization. can be nulldefaultPath
- the default path to display in the text field
-
TextFieldFileChooser
Constructor for creating a TextFieldFileChooser with customizable options.Default params set to:
selectDirectories = false defaultPath = null
- Parameters:
localeManager
- the LocaleManager for localization. can be nullselectFiles
- whether to allow file selection
-
TextFieldFileChooser
Constructor for creating a TextFieldFileChooser with customizable options.Default params set to:
selectDirectories = false
- Parameters:
localeManager
- the LocaleManager for localization. can be nullselectFiles
- whether to allow file selectiondefaultPath
- 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 nullselectFiles
- whether to allow file selectionselectDirectories
- 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 nullselectFiles
- whether to allow file selectionselectDirectories
- whether to allow directory selectiondefaultPath
- the default path to display in the text field
-
-
Method Details
-
customActionOnApprove
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
Gets the text field component.- Returns:
- the
JTextField
used in this component
-
getButton
Gets the button component.- Returns:
- the
JButton
used in this component
-
getFileChooser
Gets the file chooser instance.- Returns:
- the
FileChooser
instance used in this component
-
getText
Gets the text currently displayed in the text field.- Returns:
- the text in the text field
-
setText
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 classJComponent
- 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
Sets the tooltip text for both the text field and button.- Overrides:
setToolTipText
in classJComponent
- Parameters:
text
- the tooltip text to set
-
setFont
Sets the font for both the text field and button.- Overrides:
setFont
in classJComponent
- Parameters:
font
- theFont
to set
-
getFont
Gets the font used by the text field.- Specified by:
getFont
in interfaceMenuContainer
- Overrides:
getFont
in classComponent
- 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 componentpercentageButtonWidth
- the percentage of the total width allocated to the buttonsetSizeFunc
- a function to apply the size to the components
-
setSize
Sets the preferred size of the component. -
setPreferredSize
Sets the preferred size of the component.- Overrides:
setPreferredSize
in classJComponent
- Parameters:
preferredSize
- the preferred size of the component
-
setPreferredSize
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 componentpercentageButtonWidth
- the percentage of the total width allocated to the button
-
setMinimumSize
Sets the minimum size of the component, allocating 10% of the width to the button by default.- Overrides:
setMinimumSize
in classJComponent
- Parameters:
minimumSize
- the minimum size of the component
-
setMinimumSize
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 componentpercentageButtonWidth
- the percentage of the total width allocated to the button
-
setMaximumSize
Sets the maximum size of the component, allocating 10% of the width to the button by default.- Overrides:
setMaximumSize
in classJComponent
- Parameters:
maximumSize
- the maximum size of the component
-
setMaximumSize
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 componentpercentageButtonWidth
- the percentage of the total width allocated to the button
-