Top  | Previous | Next

system.gui.createPopupMenu

Description

Creates a new popup menu, which can then be shown over a component on a mouse event.

 

To use this function, first create a Python sequence whose entries are strings, and another sequence whose entries are function objects. The strings will be the items that are displayed in your popup menu, and when an item is clicked, its corresponding function will be run. Passing in a function of None will cause a separator line to appear in the popup menu, and the corresponding string will not be displayed. Your functions must accept an event object as an argument. See also: Functions

 

To show the popup menu, store the menu object that this function returns, and then call its show(event) function, where event is the event object for a mousePressed or mouseReleased event on the component you wish the popup menu to be shown on.

 

Best Practices. It is best to have the menu object created only once via an application specific library function. Then, call the show(event) function on both the mousePressed and mouseReleased events on your component. The reason for this is that different operating systems (Windows, Linux, MacOS) differ in when they like to show the popup menu. The show(event) function detects when the right time is to show itself, either on mouse press or release. See the examples for more.

Syntax

system.gui.createPopupMenu(itemsDict)

Parameters

PyDictionary itemsDict - A dictionary of String:Function keys to create the popup menu. You can create sub-menus by using a nested dictionary of the same type as a dictionary value.

Returns

JPopupMenu - The javax.swing.JPopupMenu that was created.

Scope

Client

system.gui.createPopupMenu(itemNames, itemFunctions)

Parameters

PySequence itemNames - A list of names to create popup menu items with.

PySequence itemFunctions - A list of functions to match up with the names.

Returns

JPopupMenu - The javax.swing.JPopupMenu that was created.

Scope

Client

Examples

This first example is a very basic to demonstrate the fundamentals of making a popup menu. Put the following script in the mouseReleased event of a component. This will only work on Windows - continue on for cross-platform instructions.

def sayHello(event):

   import system

   system.gui.messageBox("Hello World")

 

menu = system.gui.createPopupMenu({"Click Me":sayHello})

 

menu.show(event)

 

Because of the different popup-trigger settings on different operating systems, the preceding code will probably fail on Linux or a Mac. The way around this is to do the same code in both the mousePressed and mouseReleased events. In order to avoid code duplication, you'll want to factor out the code into a project script module.

 

The following, more sophisticated example shows a popup menu being used to acknowledge alarms in an alarm table by right-clicking on the table, and choosing either to acknowledge the selected alarm or all alarms. You would put this script in a project script module called app.util:

 

def getAlarmPopup():

   import system,app

 

   # This function will be the "Acknowledge" entry in the popup menu

   def ack(event):

      import system,app

      table = event.source

      selRow = table.selectedRow

      if selRow == -1:

         system.gui.warningBox("No alarm selected")

      elif table.model.getValueAt(selRow, 0) == 0:

         # In my table, the first column is the alarm's unacknowledged bit.

         system.gui.warningBox("Alarm already acknowledged")

      else:

         desc = table.model.getValueAt(selRow, 1)

         path = table.model.getValueAt(selRow, 2)

         message = "<html>Are you sure you want to acknowledge<br>%s?" % desc

         if system.gui.confirm(message,"Confirm"):

            app.auth.ackAlarm(desc,path)

            table.setSelectedRow(-1)

 

   # This function will be the "Acknowledge All" entry in the popup menu

   def ackAll(event):

      import system,app

      confirmMsg = "Are you sure you want to acknowledge all alarms?"

      if system.gui.confirm(confirmMsg,"Confirm"):

         app.auth.ackAllAlarms(event)

 

   # Finally, create the actual popup menu and return it

   alarmPopup = system.gui.createPopupMenu(

 ["Acknowledge Alarm""Acknowledge All"],

 [ack, ackAll])

   return alarmPopup

 

Now you could simply put this code in the Table's mousePressed and mouseReleased events:

menu = app.util.getAlarmPopup()

menu.show(event)