Class TextFieldButton

All Implemented Interfaces:
ImageObserver, ItemSelectable, MenuContainer, Serializable, Accessible, SwingConstants

public class TextFieldButton extends JButton
Button to pop up a dialog box containing a text field. This class arranges that the text field has the keyboard focus when it first appears. By default, the state of the text field is read and written via one of two protected methods that the user should implement: outputValue(String) and inputValue().

For example,

public class Application { JFrame appFrame; String value = "initial value"; void init() { appFrame = new JFrame("Application"); TextFieldButton tfb = new TextFieldButton("Set Value", 32, appFrame, "Value") { protected String inputValue() { return value; } protected void outputValue(String s) { value = s; } }; ... } }
If access to the text field is needed, one can use a constructor that explicitly provides the text field. For example:

 public class Application {
    JFrame appFrame;
    String value = "";

    void init() {
      JTextField tf = new JTextField(5);
      tf.setBackground(Color.GRAY);
      appFrame = new JFrame("Application");
      TextFieldButton tfb =
         new TextFieldButton("String", tf, -1, appFrame, "String Value")
         {
              protected String inputValue() {
                return value;
              }
              protected void outputValue(String s) {
                value = s;
              }
         };
      ...
    }
 }
 

IF the text field is a subclass of VTextField, the class VTextFieldButton should be used instead as methods such as outputValue(String) are not needed.

See Also:
  • Constructor Details

    • TextFieldButton

      public TextFieldButton(String label, int nchars, Component frame, String title, TextFieldButton.Mode mode)
      Constructor with a mode.
      Parameters:
      label - the button label
      nchars - the number of characters displayed by this text field
      frame - the frame on which to center a dialog box; null if none
      title - the title of the dialog box
      mode - the mode for the component
      See Also:
    • TextFieldButton

      public TextFieldButton(String label, JTextField textField, int nchars, Component frame, String title, TextFieldButton.Mode mode)
      Constructor with a mode and document.
      Parameters:
      label - the button label
      textField - this button's text-field; null if a text field should be provided.
      nchars - the number of characters displayed by this text field; negative implies that an existing textField's value should not be modified
      frame - the frame on which to center a dialog box; null if none
      title - the title of the dialog box
      mode - the mode for the component
      See Also:
    • TextFieldButton

      public TextFieldButton(String label, int nchars, Component frame, String title)
      Constructor with a default mode of USE_OUTPUT_NO_STATE.
      Parameters:
      label - the button label
      nchars - the number of characters displayed by this text field
      frame - the frame on which to center a dialog box; null if none
      title - the title of the dialog box
      See Also:
    • TextFieldButton

      public TextFieldButton(String label, JTextField textField, int nchars, Component frame, String title)
      Constructor with a default mode of USE_OUTPUT_NO_STATE and providing a text field.
      Parameters:
      label - the button label
      textField - this button's text-field; null if a text field should be provided
      nchars - the number of characters displayed by this text field; negative implies that an existing textField's value should not be modified
      frame - the frame on which to center a dialog box; null if none
      title - the title of the dialog box
      Throws:
      IllegalArgumentException - an argument was out of range (e.g., nchars was negative when textField was null)
      See Also:
  • Method Details

    • outputValue

      protected void outputValue(String value)
      Set the output value. The user of this class should override this method to store text. Whether this method is called depends on the TextFieldButton's mode.
      Parameters:
      value - the text of the component
      See Also:
    • inputValue

      protected String inputValue()
      Read the component's text from an external source. This method is expected to return the same value until the dialog box for this component is closed. Whether this method is called depends on the TextFieldButton's mode.
      Returns:
      a string containing the contents for a text area at when the text area becomes visible.
    • getTextFieldText

      public String getTextFieldText()
      Get the text of the text-field component. When the mode is USE_OUTPUT_NO_STATE (the default), the value returned is the value at the time the button was pressed. Otherwise it is the component's current value.
      Returns:
      the component's text