Class TextFieldMenuItem

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

public class TextFieldMenuItem extends JMenuItem
Menu item to pop up a dialog box containing a text field. This class arranges that the text field has the keyboard focus when it 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");
      TextFieldMenuItem tfb =
         new TextFieldMenuItem("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");
      TextFieldMenuItem tfmi =
         new TextFieldMenuItem("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 VTextFieldMenuItem should be used instead as methods such as outputValue(String) are not needed.

See Also:
  • Constructor Details

    • TextFieldMenuItem

      public TextFieldMenuItem(String label, int nchars, Component frame, String title, TextFieldButton.Mode mode)
      Constructor with a mode.
      Parameters:
      label - the menu-item label
      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:
    • TextFieldMenuItem

      public TextFieldMenuItem(String label, JTextField textField, int nchars, Component frame, String title, TextFieldButton.Mode mode)
      Constructor with a mode and text field.
      Parameters:
      label - the menu-item label
      textField - this menu item's text-field; null if a text field should be provided
      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:
    • TextFieldMenuItem

      public TextFieldMenuItem(String label, int nchars, Component frame, String title)
      Constructor with a default mode of USE_OUTPUT_NO_STATE.
      Parameters:
      label - the menu item's 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:
    • TextFieldMenuItem

      public TextFieldMenuItem(String label, JTextField textField, int nchars, Component frame, String title)
      Constructor with a default mode of TextFieldButton.Mode.USE_OUTPUT_NO_STATE.
      Parameters:
      label - the menu-item label
      textField - this menu item'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 TextFieldMenuItem'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 TextFieldMenuItem'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 menu item was pressed. Otherwise it is the component's current value.
      Returns:
      the component's text