Text Field

From Apache OpenOffice Wiki
Jump to: navigation, search



The text field, described by the service com.sun.star.awt.UnoControlEdit and its respective model - specified in com.sun.star.awt.UnoControlEditModel - is used to receive input from the user during runtime. As can be seen in the following table, most of the control settings can also be applied to the model. When a text field receives the focus by pressing the TAB key, the displayed text is selected and highlighted by default. The default cursor position within the text field is to the right of the existing text. If the user starts typing while a block of text is selected, the selected text is replaced. In some cases, the user may change the default selection behavior and set the selection manually. This is done using the com.sun.star.awt.XTextComponent interface.

Properties of com.sun.star.awt.UnoControlEditModel
EchoChar short. The UnoControlEditModel control is also commonly used for entering passwords. The property EchoChar specifies the Unicode index of the character that is displayed in the text field while the user enters the password. In this context, the MaxTextLen property is used to limit the number of characters that are typed in.
HardLineBreaks boolean. Specifies if hard line breaks are included in the text returned by the Text property.
HideInactiveSelection boolean. Specifies whether selected text within the control remains selected when the focus is not on the control. The default is true and hides the selection.
MaxTextLen short. The maximum number of characters that can be entered by the user is specified with the MaxTextLen property. A value of 0 means that there is no limitation.
MultiLine boolean. By default, a UnoControlEdit displays a single line of text. This behavior is changed by setting the property MultiLine to true.
LineEndFormat short. A value of the constant group com.sun.star.awt.LineEndFormat that defines the character denoting the line end if MultiLine is set to true.
ReadOnly boolean. In general, the text field is used for text that can be edited. It can be set read-only by setting the ReadOnly property to true.
Text The actual text displayed in a text field is controlled by the Text property.
VScroll boolean. The HScroll and VScroll properties are used to display a horizontal or vertical scroll bar to scroll the content in either direction. The properties are ignored if MultiLine is set to false.
HScroll

This example demonstrates how you can use a UnoControlEditControl:

  public XTextComponent insertEditField(XTextListener _xTextListener, XFocusListener _xFocusListener, int _nPosX, int _nPosY, int _nWidth){
  XTextComponent xTextComponent = null;
  try{
      // create a unique name by means of an own implementation...
      String sName = createUniqueName(m_xDlgModelNameContainer, "TextField");
 
      // create a controlmodel at the multiservicefactory of the dialog model... 
      Object oTFModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlEditModel");
      XMultiPropertySet xTFModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oTFModel);
 
      // Set the properties at the model - keep in mind to pass the property names in alphabetical order! 
      xTFModelMPSet.setPropertyValues(
      new String[] {"Height", "Name", "PositionX", "PositionY", "Text", "Width"},
      new Object[] { new Integer(12), sName, new Integer(_nPosX), new Integer(_nPosY), "MyText", new Integer(_nWidth)});
 
      // The controlmodel is not really available until inserted to the Dialog container
      m_xDlgModelNameContainer.insertByName(sName, oTFModel);
      XPropertySet xTFModelPSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oTFModel);
 
      // The following property may also be set with XMultiPropertySet but we
      // use the XPropertySet interface merely for reasons of demonstration 
      xTFModelPSet.setPropertyValue("EchoChar", new Short((short) '*'));
      XControl xTFControl = m_xDlgContainer.getControl(sName);
 
      // add a textlistener that is notified on each change of the controlvalue... 
      xTextComponent = (XTextComponent) UnoRuntime.queryInterface(XTextComponent.class, xTFControl);
      XWindow xTFWindow = (XWindow) UnoRuntime.queryInterface(XWindow.class, xTFControl);
      xTFWindow.addFocusListener(_xFocusListener);
      xTextComponent.addTextListener(_xTextListener);
      xTFWindow.addKeyListener(this);
  }catch (com.sun.star.uno.Exception ex){
      /* perform individual exception handling here.
       * Possible exception types are:
       * com.sun.star.lang.IllegalArgumentException,
       * com.sun.star.lang.WrappedTargetException,
       * com.sun.star.container.ElementExistException,
       * com.sun.star.beans.PropertyVetoException,
       * com.sun.star.beans.UnknownPropertyException,
       * com.sun.star.uno.Exception
       */
      ex.printStackTrace(System.out);
  }
      return xTextComponent;
  }

The text listener must implement the method textChanged:

  public void textChanged(TextEvent textEvent) {
  try{
      // get the control that has fired the event, 
      XControl xControl = (XControl) UnoRuntime.queryInterface(XControl.class, textEvent.Source);
      XControlModel xControlModel = xControl.getModel();
      XPropertySet xPSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xControlModel);
      String sName = (String) xPSet.getPropertyValue("Name");
      // just in case the listener has been added to several controls,
      // we make sure we refer to the right one
      if (sName.equals("TextField1")){
          String sText = (String) xPSet.getPropertyValue("Text");
          System.out.println(sText);
          // insert your code here to validate the text of the control...
      } 
  }catch (com.sun.star.uno.Exception ex){
      /* perform individual exception handling here.
       * Possible exception types are:
       * com.sun.star.lang.WrappedTargetException,
       * com.sun.star.beans.UnknownPropertyException,
       * com.sun.star.uno.Exception
       */
      ex.printStackTrace(System.out);
  }}

The control that supports the interface com.sun.star.awt.XTextComponent offers additional methods to query and set selections and insert part texts in the control.

Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages