Class LabeledTextField

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible

public class LabeledTextField extends JPanel
LabeledTextField is a combo component which includes text field and an optional JLabel in the front and another optional AbstractButton at the end.
See Also:
  • Field Details

  • Constructor Details

    • LabeledTextField

      public LabeledTextField()
    • LabeledTextField

      public LabeledTextField(Icon icon)
    • LabeledTextField

      public LabeledTextField(Icon icon, String labelText)
  • Method Details

    • initComponent

      protected void initComponent()
    • showContextMenu

      protected void showContextMenu()
      Shows the context menu.
    • calculateContextMenuLocation

      protected Point calculateContextMenuLocation()
      Calculates the locatioin of the context menu.
      Returns:
      the upper-left corner location.
      Since:
      3.4.2
    • customizePopupMenu

      protected void customizePopupMenu(JPopupMenu menu)
      Customizes the popup menu.
      Parameters:
      menu - the menu to customize
      Since:
      3.4.1
    • initLayout

      protected void initLayout(JLabel label, JTextField field, AbstractButton button)
      Setup the layout of the components. By default, we used a border layout with label first, field in the center and button last.
      Parameters:
      label - the label
      field - the text field.
      button - the button
    • isShowHintTextWhenFocused

      public boolean isShowHintTextWhenFocused()
      Checks if the hint text will still be shown when the text field has focus. By default, the hint text is only shown when the text field doesn't have focus.
      Returns:
      true or false.
      Since:
      3.3.6
    • setShowHintTextWhenFocused

      public void setShowHintTextWhenFocused(boolean showHintTextWhenFocused)
      Sets the flag if the hint text will still be shown when the text field has focus. By default, the hint text is only shown when the text field doesn't have focus. If you set it to true, the hint text will always be shown regardless if the text field has focus.
      Parameters:
      showHintTextWhenFocused - true or false.
      Since:
      3.3.6
    • createTextField

      protected JTextField createTextField()
      Creates a text field. By default it will return a JTextField with opaque set to false. Subclass can override this method to create their own text field such as JFormattedTextField.
      Returns:
      a text field.
    • createContextMenu

      protected JidePopupMenu createContextMenu()
      Creates a context menu. The context menu will be shown when user clicks on the label.
      Returns:
      a context menu.
    • updateUI

      public void updateUI()
      Overrides:
      updateUI in class JPanel
    • createButton

      protected AbstractButton createButton()
      Creates the button that appears after the text field. By default it returns null so there is no button. Subclass can override it to create their own button. A typical usage of this is to create a browse button to browse a file or directory.
      Returns:
      the button.
    • createLabel

      protected JLabel createLabel()
      Creates the label that appears before the text field. By default, it only has a search icon.
      Returns:
      the label.
    • setLabelText

      public void setLabelText(String text)
      Sets the text that appears before the text field.
      Parameters:
      text - the text that appears before the text field.
    • getLabelText

      public String getLabelText()
      Gets the text that appears before the text field.
      Returns:
      the text that appears before the text field. By default it's null, meaning no text.
    • setIcon

      public void setIcon(Icon icon)
      Sets the icon that appears before the text field.
      Parameters:
      icon - the icon that appears before the text field.
    • getIcon

      public Icon getIcon()
      Gets the icon that appears before the text field.
      Returns:
      the icon that appears before the text field.
    • getLabel

      public JLabel getLabel()
      Gets the JLabel that appears before text field.
      Returns:
      the JLabel that appears before text field.
    • getButton

      public AbstractButton getButton()
      Gets the AbstractButton that appears after text field.
      Returns:
      the AbstractButton that appears after text field.
    • setColumns

      public void setColumns(int columns)
      Sets the number of columns in this TextField, and then invalidate the layout.
      Parameters:
      columns - the number of columns for this text field.
    • setText

      public void setText(String text)
      Sets the text in this TextField.
      Parameters:
      text - the new text in this TextField.
    • getText

      public String getText()
      Gets the text in this TextField.
      Returns:
      the text in this TextField.
    • getTextField

      public JTextField getTextField()
      Gets the actual text field.
      Returns:
      the actual text field.
    • setEnabled

      public void setEnabled(boolean enabled)
      Overrides:
      setEnabled in class JComponent
    • getBaseline

      public int getBaseline(int width, int height)
      Overrides:
      getBaseline in class JComponent
    • getHintText

      public String getHintText()
      Gets the hint text when the field is empty and not focused.
      Returns:
      the hint text.
    • setHintText

      public void setHintText(String hintText)
      Sets the hint text.
      Parameters:
      hintText - the new hint text.
    • getPopupMenuCustomizer

      public LabeledTextField.PopupMenuCustomizer getPopupMenuCustomizer()
      Gets the PopupMenuCustomizer.
      Returns:
      the PopupMenuCustomizer.
    • setPopupMenuCustomizer

      public void setPopupMenuCustomizer(LabeledTextField.PopupMenuCustomizer customizer)
      Sets the PopupMenuCustomizer. PopupMenuCustomizer can be used to do customize the popup menu for the LabeledTextField.

      PopupMenuCustomizer has a customize method. The popup menu of this menu will be passed in. You can add/remove/change the menu items in customize method. For example,

       field.setPopupMenuCustomizer(new LabeledTextField.PopupMenuCustomizer() {
           void customize(LabeledTextField field, JPopupMenu menu) {
               menu.removeAll();
               menu.add(new JMenuItem("..."));
               menu.add(new JMenuItem("..."));
           }
       }
       
      If the menu is never used, the two add methods will never be called thus improve the performance.
      Parameters:
      customizer - the PopupMenuCustomizer
    • getContextMenuKeyStroke

      public KeyStroke getContextMenuKeyStroke()
      Gets the keystroke that will bring up the context menu. If you never set it before, it will return SHIFT-F10 for operating systems other than Mac OS X.
      Returns:
      the keystroke that will bring up the context menu.
    • setContextMenuKeyStroke

      public void setContextMenuKeyStroke(KeyStroke contextMenuKeyStroke)
      Changes the keystroke that brings up the context menu which is normally shown when user clicks on the label icon before the text field.
      Parameters:
      contextMenuKeyStroke - the new keystroke to bring up the context menu.