Check Box

From Apache OpenOffice Wiki
Jump to: navigation, search



The check box control model com.sun.star.awt.UnoControlCheckBoxModelis used in groups to display multiple choices. When a check box is selected it displays a check mark. Check boxes work independently of each other. A user can select any number or combination of check boxes. The State property of the model service com.sun.star.awt.UnoControlCheckBoxModel defines three values, where 0 is not checked, 1 is checked, and 2 is undetermined. You can enable the tri-state mode of a check box by setting the TriState property to True. A tri-state check box used to give the user the option of setting or unsetting an option.

  public XCheckBox insertCheckBox(XItemListener _xItemListener, int _nPosX, int _nPosY, int _nWidth){
  XCheckBox xCheckBox = null;
  try{
      // create a unique name by means of an own implementation...
      String sName = createUniqueName(m_xDlgModelNameContainer, "CheckBox");
 
      // create a controlmodel at the multiservicefactory of the dialog model... 
      Object oCBModel = m_xMSFDialogModel.createInstance("com.sun.star.awt.UnoControlCheckBoxModel");
 
      // Set the properties at the model - keep in mind to pass the property names in alphabetical order!
      XMultiPropertySet xCBMPSet = (XMultiPropertySet) UnoRuntime.queryInterface(XMultiPropertySet.class, oCBModel);
      xCBMPSet.setPropertyValues( 
      new String[] {"Height", "Label", "Name", "PositionX", "PositionY", "Width" } ,
      new Object[] {new Integer(8), "~Include page number", sName, new Integer(_nPosX), new Integer(_nPosY), new Integer(_nWidth)});
 
      // The following property may also be set with XMultiPropertySet but we
      // use the XPropertySet interface merely for reasons of demonstration 
      XPropertySet xCBModelPSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xCBMPSet);
      xCBModelPSet.setPropertyValue("TriState", Boolean.TRUE);
      xCBModelPSet.setPropertyValue("State", new Short((short) 1)); 
 
      // add the model to the NameContainer of the dialog model
      m_xDlgModelNameContainer.insertByName(sName, oCBModel);
      XControl xCBControl = m_xDlgContainer.getControl(sName);
      xCheckBox = (XCheckBox) UnoRuntime.queryInterface(XCheckBox.class, xCBControl);
      // An ActionListener will be notified on the activation of the button...
      xCheckBox.addItemListener(_xItemListener);
  }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 xCheckBox;
  }

In this example, a com.sun.star.awt.XItemListener is attached to the checkbox control. This listener is notified on each change of the State property in the control model. Listeners on check boxes are often used to enable or disable controls whose functionality is dependent on the state of a checkbox:

  public void itemStateChanged(ItemEvent itemEvent) {
  try{
      // retrieve the control that the event has been invoked at...
      XCheckBox xCheckBox = (XCheckBox) UnoRuntime.queryInterface(XCheckBox.class, itemEvent.Source);
      // retrieve the control that we want to disable or enable
      XControl xControl = (XControl) UnoRuntime.queryInterface(XControl.class, m_xDlgContainer.getControl("CommandButton1"));
      XPropertySet xModelPropertySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xControl.getModel());
      short nState = xCheckBox.getState();
      boolean bdoEnable = true;
      switch (nState){
          case 1: // checked 
              bdoEnable = true;
              break;
          case 0: // not checked 
          case 2: // don't know 
              bdoEnable = false;
              break;
      }
      // Alternatively we could have done it also this way:
      // bdoEnable = (nState == 1);
      xModelPropertySet.setPropertyValue("Enabled", new Boolean(bdoEnable));
  }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.beans.UnknownPropertyException,
       * com.sun.star.beans.PropertyVetoException
       * com.sun.star.uno.Exception
       */
      ex.printStackTrace(System.out);
  }}

A checkbox may also display images similar to a button as described in Command Button.

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