Message Box

From Apache OpenOffice Wiki
Jump to: navigation, search



Message boxes contain a defined message and title that may be combined with predefined icons and buttons. Again the central instance to create a Message box is the service com.sun.star.awt.Toolkit. It serves as a factory that exports the interface com.sun.star.awt.XMessageBoxFactory. Its method createMessageBox() allows the creation of message boxes in various defined facets.

  • The first parameter of createMessageBox() denotes the peer of the parent window. In analogy to all Apache OpenOffice windows the peer of the window parent must be conveyed.
  • The second parameter aPosSize may be empty (but not null).
  • The third parameter aType describes the special usecase of the message box. The interface description lists a bunch of defined strings like "errorbox" or "querybox". The message box type is than differentiated by its contained icon.
  • Depending on the use case, different combinations of buttons in the message box are possible and reflected by a value of the constants group com.sun.star.awt.MessageBoxButtons. This is the fourth parameter aButtons.
  • The last two parameters reflect the title (aTitle) and the message (aMessage) of the message box.

This example creates and executes a message box.

  /** shows an error messagebox
   * @param _xParentWindowPeer the windowpeer of the parent window
   * @param _sTitle the title of the messagebox
   * @param _sMessage the message of the messagebox
   */
  public void showErrorMessageBox(XWindowPeer _xParentWindowPeer, String _sTitle, String _sMessage){
  XComponent xComponent = null; 
  try {
      Object oToolkit = m_xMCF.createInstanceWithContext("com.sun.star.awt.Toolkit", m_xContext);
      XMessageBoxFactory xMessageBoxFactory = (XMessageBoxFactory) UnoRuntime.queryInterface(XMessageBoxFactory.class, oToolkit);
      // rectangle may be empty if position is in the center of the parent peer
      Rectangle aRectangle = new Rectangle();
      XMessageBox xMessageBox = xMessageBoxFactory.createMessageBox(_xParentWindowPeer, aRectangle, "errorbox", com.sun.star.awt.MessageBoxButtons.BUTTONS_OK, _sTitle, _sMessage);
      xComponent = (XComponent) UnoRuntime.queryInterface(XComponent.class, xMessageBox);
      if (xMessageBox != null){
          short nResult = xMessageBox.execute();
      }
  } catch (com.sun.star.uno.Exception ex) {
      ex.printStackTrace(System.out);
  }
  finally{
      //make sure always to dispose the component and free the memory!
      if (xComponent != null){
          xComponent.dispose();
      }
  }}
Content on this page is licensed under the Public Documentation License (PDL).
Personal tools
In other languages