Package sim.util.gui

Class PropertyField

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable

public class PropertyField extends JComponent
A simple class designed to allow the user to modify a property in the form of a string, number, boolean value, or option. PropertyField lets you control the values which the user sets by subclassing the class and overriding the newValue(val) method filters all newly user-set values and "corrects" them. Programmatically set values (by calling setValue(...)) are not filtered through newValue by default. If you need to filter, you should do setValue(newValue(val));

You can optionally specify how the string will be presented to the user: as a text field, as a text field with a slider (requiring certain numerical constraints on the text field), as a list of options (also requiring certain numerical constraints), as a check box (requiring the string to hold boolean values ("true" or "false"), or as a read-only field with a button to press (which in turn calls the viewProperty() method, which you may override).

The specifics about how to present the user with these options is described in the constructor documentation and in the documentation for setValues(...).

PropertyFields can also be set to be either read-only or read/write by the user. When the user edits a read/write PropertyField, the text field changes color. If the user then presses RETURN, the result is submitted to newValue(...). If the user presses ESCAPE, the result is cancelled and reset.

See Also:
  • Field Details

  • Constructor Details

    • PropertyField

      public PropertyField()
      Constructs a PropertyField as just a writeable, empty text field.
    • PropertyField

      public PropertyField(String initialValue)
      Constructs a PropertyField as a writeable text field with the provided initial value.
    • PropertyField

      public PropertyField(String initialValue, boolean isReadWrite)
      Constructs a PropertyField as a text field with the provided initial value, either writeable or not.
    • PropertyField

      public PropertyField(String label, String initialValue)
      Constructs a labelled PropertyField as a writeable text field with the provided initial value.
    • PropertyField

      public PropertyField(String label, String initialValue, boolean isReadWrite)
      Constructs a labelled PropertyField as a text field with the provided initial value, either writeable or not.
    • PropertyField

      public PropertyField(String label, String initialValue, boolean isReadWrite, Object domain, int show)
      Constructs a PropertyField with an optional label, an initial value, a "writeable" flag, an optional domain (for the slider and list options), and a display form (checkboxes, view buttons, text fields, sliders, or lists).
      • If show is SHOW_CHECKBOX, a checkbox will be shown (expecting "true" and "false" string values); pass in null for domain.
      • If show is SHOW_VIEWBUTTON, a view button will be shown (expecting a true object); pass in null for domain.
      • If show is SHOW_TEXTFIELD, a textfield will be shown; pass in null for domain.
      • If show is SHOW_SLIDER, both a textfield and a slider will be shown; the initialValue must be a number, and domain must be a sim.util.Interval. In this case, newValue(...) will be passed a String holding a number in the Interval range and must return a number. PropertyField will automatically make certain that the numbers are integral or real-valued; you do not need to check this so long as the Interval returns Longs or Doubles respectively. If isReadWrite is false, then the slider is not shown -- only the textfield.
      • If show is SHOW_LIST, a list will be shown; the initialValue must be an integer specifying the number in the list, and domain must be an array of Objects (strings, whatnot) or a java.util.List providing the objects in the list. In this case, newValue(...) will be passed a String holding a number; that number is the index in the list which the user has checked. newValue(...) must also return a String with the desired index for the list to be set to.
  • Method Details

    • getField

      public JTextField getField()
    • setEditedColor

      public void setEditedColor(Color c)
    • getEditedColor

      public Color getEditedColor()
    • submit

      public void submit()
      Commits to the current setting of the propertyField, filtering it through newValue.
    • update

      public void update()
      Reverts the property field to its previous string value WITHOUT calling newValue()
    • setValue

      public void setValue(String val)
      Sets the value, not filtering it through newValue(val) first.
    • getValue

      public String getValue()
      Returns the most recently set value.
    • newValue

      public String newValue(String newValue)
      Override this to be informed when a new value has been set. The return value should be the value you want the display to show instead.
    • viewProperty

      public void viewProperty()
      Override this to be informed when a property is to be viewed in its own inspector because the user pressed the "view" button.
    • setToolTipText

      public void setToolTipText(String text)
      Overrides:
      setToolTipText in class JComponent
    • getMinimumSize

      public Dimension getMinimumSize()
      Overrides:
      getMinimumSize in class JComponent
    • getPreferredSize

      public Dimension getPreferredSize()
      Overrides:
      getPreferredSize in class JComponent
    • setEnabled

      public void setEnabled(boolean b)
      Overrides:
      setEnabled in class JComponent