Class TextAreaMenuItem

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

public class TextAreaMenuItem extends JMenuItem
MenuItem to pop up a dialog box containing a text area. This class arranges that the text area has the keyboard focus when the text area first appears. By default, the state of the text area 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 = new JFrame("Application");
    String value = "initial value";
    TextFieldMenuItem tfmi =
         new TextAreaMenuItem("Set Value", 32, 10,
                              appFrame, "Value")
         {
              protected String inputValue() {
                return value;
              }
              protected void outputValue(String s) {
                value = s;
              }
         };
    ...
 }
 
If access to the text area is needed, one can use a constructor that explicitly provides the text are. For example:

 public class Application {
    JFrame appFrame;
    String value = "";
    void init() {
      JTextArea ta = new JTextArea(5, 40);
      ta.setBackground(Color.GRAY);
      appFrame = new JFrame("Application");
      TextAreaMenuItem tami =
         new TextAreadMenuItem("String", tami, -1, -1,
                               appFrame, "String Value")
         {
              protected String inputValue() {
                return value;
              }
              protected void outputValue(String s) {
                value = s;
              }
         };
      ...
    }
 }
 
The values of -1 in the constructor indicate that the rows and columns in the text area should not be changed by the constructor.
See Also:
  • Constructor Details

    • TextAreaMenuItem

      public TextAreaMenuItem(String label, int rows, int cols, Component frame, String title, TextAreaButton.Mode mode)
      Constructor with mode.
      Parameters:
      label - the menu-item label
      rows - the number of rows for text (must not be negative)
      cols - the number of columns for text (must not be negative)
      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:
    • TextAreaMenuItem

      public TextAreaMenuItem(String label, JTextArea textArea, int rows, int cols, Component frame, String title, TextAreaButton.Mode mode)
      Constructor with mode and specifying a JTextArea to use.
      Parameters:
      label - the menu-item label
      textArea - the text area this button will cause to be displayed
      rows - the number of rows for text (must not be negative)
      cols - the number of columns for text (must not be negative)
      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:
    • TextAreaMenuItem

      public TextAreaMenuItem(String label, int rows, int cols, Component frame, String title)
      Constructor with a default mode of USE_OUTPUT_NO_STATE.
      Parameters:
      label - the menu-item label
      rows - the number of rows for text
      cols - the number of columns for text
      frame - the frame on which to center a dialog box; null if none
      title - the title of the dialog box
    • TextAreaMenuItem

      public TextAreaMenuItem(String label, JTextArea textArea, int rows, int cols, Component frame, String title)
      Constructor with a default mode of USE_OUTPUT_NO_STATE, specifying a text area.
      Parameters:
      label - the menu-item label
      textArea - the text area this menu item will cause to be displayed
      rows - the number of rows for text; a negative value if the value provided by textArea should not be changed
      cols - the number of columns for text; a negative value if the value provided by textArea should not be changed
      frame - the frame on which to center a dialog box; null if none
      title - the title of the dialog box
  • 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 TextAreaMenuItem'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 TextAreaMenuItem's mode.
      Returns:
      a string containing the contents for a text area at when the text area becomes visible.
    • getTextAreaText

      public String getTextAreaText()
      Get the text of the text-area 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