Class SignatureComponent

java.lang.Object
com.codename1.ui.Component
com.codename1.ui.Container
com.codename1.components.SignatureComponent
All Implemented Interfaces:
Animation, Editable, ActionSource<ActionEvent>, StyleListener, Iterable<Component>

public class SignatureComponent extends Container implements ActionSource<ActionEvent>

A component to allow a user to enter their signature. This is just a button that, when pressed, will pop up a dialog where the user can draw their signature with their finger. The user is given the option to save/reset/cancel the signature. On save, the #signatureImage property will be set with a full-size image of the signature, and the "icon" on the button will show a thumbnail of the image.

Example Usage

Form hi = new Form("Signature Component");
hi.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
hi.add("Enter Your Name:");
hi.add(new TextField());
hi.add("Signature:");
SignatureComponent sig = new SignatureComponent();
sig.addActionListener((evt)-> {
    System.out.println("The signature was changed");
    Image img = sig.getSignatureImage();
    // Now we can do whatever we want with the image of this signature.
});
hi.addComponent(sig);
hi.show();

Screenshots

Video Demo

Source available here

.

Styles

You can customize the styles of various aspects of the Signature component using the following Styles (UIIDs) in the theme:

  • SignatureButton - The style for the main signature component button.

  • SignatureButtonBox - A style to specify the "X" and "Box" that is drawn around the signature in the button.

  • SignaturePanel - The panel that the user actually draws the signature in.

  • SignaturePanelBox - The box and "X" in the SignaturePanel. Uses only the Style#getFgColor() property.

  • SignaturePanelSignature - The signature that is drawn by the user. Uses only the Style#getFgColor() property.

  • Constructor Details

    • SignatureComponent

      public SignatureComponent()
      Creates a new signature component.
  • Method Details

    • onSignatureReset

      protected void onSignatureReset()

      A hook that can be overridden by subclasses to be notified when the user resets the signature in the signature dialog.

      NOTE: Use of this hook to clear the current image in the signature component is discouraged. The intention of the signature component's internal dialog is to provide a staging area for the user to draw their signature. Changes should only take effect in the app when the user commits their changes by pressing "OK" or "Cancel". The "Reset" button in the signature dialog is intended to only allow the user to fix a mistake and start over. Using this hook to cause a change in application state (by, for example, calling #setSignatureImage(com.codename1.ui.Image) with a null argument}) may confuse the user.

      If you want to provide the user with a mechanism to "clear" the signature from the signature component, you should add a button to your form which, when pressed, will remove the image by calling #setSignatureImage(com.codename1.ui.Image).

    • addActionListener

      public void addActionListener(ActionListener<ActionEvent> l)

      Adds a listener to be notified when the signature image is changed.

      Parameters
      • l
      Specified by:
      addActionListener in interface ActionSource<ActionEvent>
    • removeActionListener

      public void removeActionListener(ActionListener<ActionEvent> l)

      Removes a listener from being notified when signature image is changed.

      Parameters
      • l
      Specified by:
      removeActionListener in interface ActionSource<ActionEvent>
    • fireActionEvent

      protected void fireActionEvent()
      Fires an event to all listeners to notify them that the signature image has been changed.
    • initComponent

      protected void initComponent()
      Overridden to register the icon animation when the field is added to the form.
      Overrides:
      initComponent in class Component
    • deinitialize

      protected void deinitialize()
      Overridden to deregister the icon animation when the field is removed from the form.
      Overrides:
      deinitialize in class Component
    • calcPreferredSize

      protected Dimension calcPreferredSize()
      Calculates the preferred size of the button itself (not the signature canvas... but the button you press to show the signature panel).
      Overrides:
      calcPreferredSize in class Container
    • getSignatureImage

      public Image getSignatureImage()
      Gets the image of the signature - or null if no signature has been drawn.
    • setSignatureImage

      public void setSignatureImage(Image img)

      Sets the signature image for this field. This will also scale the image and show it as the icon for the button.

      Parameters
      • img: The image to set as the signature image.
    • getSignaturePanel

      public Component getSignaturePanel()

      Get the component that is the actual panel for drawing a signature. The component can be used instead of the SignatureComponent if an embedded signature is needed.

      Use the clearSignaturePanel() and the getSignatureImage() functions to work with this component.

    • clearSignaturePanel

      public void clearSignaturePanel()
      Clear the signature image and allow it to draw a new one. Use only if you use the signature panel component from the getSignaturePanel() method.