List Box

From Apache OpenOffice Wiki
Jump to: navigation, search



The list box control com.sun.star.awt.UnoControlListBox displays a list of items that the user can select one or more of. If the number of items exceeds what can be displayed in the list box, scroll bars automatically appear on the control. The model of a list box supports the service com.sun.star.awt.UnoControlListBoxModel :

Properties of com.sun.star.awt.UnoControlListBoxModel
Dropdown boolean. If the Dropdown property is set to true, the list of items is displayed in a drop-down box.
LineCount short. If the Dropdown property is set to true, the maximum number of line counts in the drop- down box are specified with the LineCount property.
MultiSelection boolean. If the MultiSelection property is set to true, more than one entry can be selected. This property is ignored if Dropdown is set to true.
StringItemList string[]. A sequence of strings containing the actual list of items within the list box.
SelectedItems short[]. A sequence of strings containing the actual list of indices of all selected items.

The list box allows you to register a com.sun.star.awt.XItemListener as well as a com.sun.star.awt.XActionListener. Double-clicking a list box item will invoke the method actionPerformed() of the action listener. If items are selected with a single click or even programmatically, the method itemStateChanged() is called when an item listener is registered at the list box control.

The list box control that supports the interface com.sun.star.awt.XListBox offers more convenient functions than the list box model. For example, it offers the method selectItemPos( [in] short nPos,[in] boolean bSelect ) to select or deselect a single item in the list box. As can be seen in the following example, to achieve the same result with the model, a sequence of all selected list box item indices must be assigned.

  public XListBox insertListBox(int _nPosX, int _nPosY, int _nWidth, int _nStep, String[] _sStringItemList){
  XListBox xListBox = null;
  try{
      // create a unique name by means of an own implementation...
      String sName = createUniqueName(m_xDlgModelNameContainer, "ListBox");
 
      // create a controlmodel at the multiservicefactory of the dialog model... 
      Object oListBoxModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlListBoxModel");
      XMultiPropertySet xLBModelMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oListBoxModel);
      // Set the properties at the model - keep in mind to pass the property names in alphabetical order!
      xLBModelMPSet.setPropertyValues( 
      new String[] {"Dropdown", "Height", "Name", "PositionX", "PositionY", "Step", "StringItemList", "Width" } ,
      new Object[] {Boolean.TRUE, new Integer(12), sName, new Integer(_nPosX), new Integer(_nPosY), new Integer(_nStep), _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 xLBModelPSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xLBModelMPSet);
      xLBModelPSet.setPropertyValue("MultiSelection", Boolean.TRUE);
      short[] nSelItems = new short[] {(short) 1, (short) 3};
      xLBModelPSet.setPropertyValue("SelectedItems", nSelItems);
      // add the model to the NameContainer of the dialog model
      m_xDlgModelNameContainer.insertByName(sName, xLBModelMPSet);
      XControl xControl = m_xDlgContainer.getControl(sName);
      // retrieve a ListBox that is more convenient to work with than the Model of the ListBox...
      xListBox = (XListBox) UnoRuntime.queryInterface(XListBox.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 xListBox;
  }
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages