Combo Box

From Apache OpenOffice Wiki
< Documentation‎ | DevGuide
Revision as of 12:44, 22 December 2020 by DiGro (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search



A combo box control presents a list of items to the user. It also contains a text field allowing the user to input text that is not in the list. A combo box is used when there is a list of suggested choices, whereas a list box is used when the user's input is limited only to the list. The features and properties of a combo box and a list box are similar. As can be seen in com.sun.star.awt.UnoControlComboBox the combo box includes the functionality of a com.sun.star.awt.UnoControlEdit, which also allows you to add an com.sun.star.awt.XTextListener to the combo box. The text displayed in the field of the combo box can be controlled by the Text property of the combo box model that supports the service com.sun.star.awt.UnoControlComboBoxModel. Just like in the list box, the actual list of items is accessible through the StringItemList property. A useful feature of the model is the automatic word completion that can be activated by setting the property Autocomplete to true.

You can control the items in a combo box via the interface com.sun.star.awt.XComboBox at the control, which offers a more convenient access to the control's functionality.

  public XComboBox insertComboBox(int _nPosX, int _nPosY, int _nWidth){
  XComboBox xComboBox = null;
  try{
      // create a unique name by means of an own implementation...
      String sName = createUniqueName(m_xDlgModelNameContainer, "ComboBox");
 
      String[] sStringItemList = new String[]{"First Entry", "Second Entry", "Third Entry", "Fourth Entry"};
 
      // create a controlmodel at the multiservicefactory of the dialog model... 
      Object oComboBoxModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlComboBoxModel");
      XMultiPropertySet xCbBModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oComboBoxModel);
      // Set the properties at the model - keep in mind to pass the property names in alphabetical order!
      xCbBModelMPSet.setPropertyValues( 
      new String[] {"Dropdown", "Height", "Name", "PositionX", "PositionY", "StringItemList", "Width" } ,
      new Object[] {Boolean.TRUE, new Integer(12), sName, new Integer(_nPosX), new Integer(_nPosY), sStringItemList, new Integer(_nWidth)}); 
 
      // The following property may also be set with XMultiPropertySet but we
      // use the XPropertySet interface merely for reasons of demonstration 
      XPropertySet xCbBModelPSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xCbBModelMPSet);
      xCbBModelPSet.setPropertyValue("MaxTextLen", new Short((short) 10));
      xCbBModelPSet.setPropertyValue("ReadOnly", Boolean.FALSE);
 
      // add the model to the NameContainer of the dialog model
      m_xDlgModelNameContainer.insertByName(sName, xCbBModelMPSet);
      XControl xControl = m_xDlgContainer.getControl(sName);
 
      // retrieve a ListBox that is more convenient to work with than the Model of the ListBox...
      xComboBox = (XComboBox) UnoRuntime.queryInterface(XComboBox.class, xControl);
  }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 xComboBox;
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages