I found some code which implements a listener. It adds a count to a label when a button is clicked.
I then tried adding a ListBox and attached the same listener code to the ListBox.
This essentially works and would form the basis of what I want to do. HOWEVER.... I don't understand why I only need to click once on the button to increment the count, but I need to double-click on an item in the ListBox to achieve the same thing. I would prefer a single-click on the ListBox.
Can anyone provide any guidance
- Code: Select all Expand viewCollapse view
import uno
import unohelper
from com.sun.star.awt import XActionListener
class MyActionListener( unohelper.Base, XActionListener ):
def __init__(self, labelControl, prefix ):
self.nCount = 0
self.labelControl = labelControl
self.prefix = prefix
def actionPerformed(self, actionEvent):
# increase click counter
self.nCount = self.nCount + 1
self.labelControl.setText( self.prefix + str( self.nCount ) )
def createDialog():
"""Opens a dialog with a push button and a label, clicking the button increases the label counter."""
try:
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
# create the dialog model and set the properties
dialogModel = smgr.createInstanceWithContext("com.sun.star.awt.UnoControlDialogModel", ctx)
dialogModel.PositionX = 10
dialogModel.PositionY = 10
dialogModel.Width = 200
dialogModel.Height = 100
dialogModel.Title = "Runtime Dialog Demo"
# create listbox
listBoxModel = dialogModel.createInstance("com.sun.star.awt.UnoControlListBoxModel" )
listBoxModel.PositionX = 10
listBoxModel.PositionY = 5
listBoxModel.Width = 100
listBoxModel.Height = 40
listBoxModel.Name = "myListBoxName"
listBoxModel.StringItemList = ('a','b','c')
# create the button model and set the properties
buttonModel = dialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel" )
buttonModel.PositionX = 50
buttonModel.PositionY = 50
buttonModel.Width = 50
buttonModel.Height = 14
buttonModel.Name = "myButtonName"
buttonModel.Label = "Click Me"
# create the label model and set the properties
labelModel = dialogModel.createInstance( "com.sun.star.awt.UnoControlFixedTextModel" )
labelModel.PositionX = 10
labelModel.PositionY = 70
labelModel.Width = 100
labelModel.Height = 14
labelModel.Name = "myLabelName"
labelModel.Label = "Clicks "
# insert the control models into the dialog model
dialogModel.insertByName( "myButtonName", buttonModel)
dialogModel.insertByName( "myLabelName", labelModel)
dialogModel.insertByName( "myListBoxName", listBoxModel)
# create the dialog control and set the model
controlContainer = smgr.createInstanceWithContext("com.sun.star.awt.UnoControlDialog", ctx)
controlContainer.setModel(dialogModel)
oBox = controlContainer.getControl("myListBoxName")
oLabel = controlContainer.getControl("myLabelName")
oButton = controlContainer.getControl("myButtonName")
oBox.addItem('d',4)
# add the action listener
oButton.addActionListener(MyActionListener( oLabel,labelModel.Label ))
oBox.addActionListener(MyActionListener( oLabel,labelModel.Label ))
# create a peer
toolkit = smgr.createInstanceWithContext( "com.sun.star.awt.ExtToolkit", ctx)
controlContainer.setVisible(False)
controlContainer.createPeer(toolkit, None)
# execute it
controlContainer.execute()
# dispose the dialog
controlContainer.dispose()
except:
pass
g_exportedScripts = createDialog,