Script on mouseClicked Button requires 2 clicks to run

Hello all!

I have a script that is supposed to read the selected row, grab a cell based on the column header I give, and then run a named query to update the database using that cells data as a parameter.

The script will run fine, although it doesn't seem to fire until I click it twice. I have tried changing the event from a mouseReleased to mouseClicked and still have the same issue. I will paste the script below; it is pulling the data from a Power Table. The table pulls from a named query.

Script:

table = event.source.parent.getComponent('Power Table')
 
selectedIndices = table.getSelectedRows()
 
if len(selectedIndices) > 0:
     
    for index in selectedIndices:
     
        taskId = table.data.getValueAt(index, "task_id1")   
     
      	system.tag.writeBlocking("[default]placeholder", taskId)
      	
      	params = {"TID":event.source.parent.getComponent('Label 5').text}
      	
      	system.db.runNamedQuery("Complete Task", params)
      	
else:
	print "oops!"

Please do let me know if I need any more information, thank you!

Don't run this on the mouse click because the event fires before the index is selected. Run this from the propertyChange event handler with a selectedIndex qualifier.

2 Likes
1 Like

Thank you!

Although, it seems to be doing the same thing. Here is the script as it stands now.

if event.propertyName == 'selectedIndex':
	# Grab a reference to the table.
	table = event.source.parent.getComponent('Power Table')
	 
	selectedIndices = table.getSelectedRows()
	 
	# If the length of selectedIndices is greater than 0, then at least one row is selected.
	if len(selectedIndices) > 0:
	     
	    for index in selectedIndices:
	     
	        taskId = table.data.getValueAt(index, "task_id1")   
	     
	        # We can do something with the value as we iterate, or append to a list and
	        # do something with the entire list after the for-loop completes.
	      	system.tag.writeBlocking("[default]placeholder", taskId)
	     
	      	params = {"TID":event.source.parent.getComponent('Label 5').text}
	      	
	      	system.db.runNamedQuery("Complete Task", params)
	      	
	else:
		print "oops!"

Is this being fired from the table, or some other component?

It is being fired from a button! I apologize for not stating that earlier.

Then use the actionPerformed event handler with no qualifier.

Thank you, would I need to use the event object property source?

I tried running it in the actionPerformed as well but same result. I did not use if event.propertyName == 'selectedIndex': as well

I don't see any reason why this wouldn't work on the first click from the actionPerformed event handler.

table = event.source.parent.getComponent('Power Table')
	 
	selectedIndices = table.getSelectedRows()
	 
	# If the length of selectedIndices is greater than 0, then at least one row is selected.
	if len(selectedIndices) > 0:
	     
	    for index in selectedIndices:
	     
	        taskId = table.data.getValueAt(index, "task_id1")   
	     
	        # We can do something with the value as we iterate, or append to a list and
	        # do something with the entire list after the for-loop completes.
	      	system.tag.writeBlocking("[default]placeholder", taskId)
	     
	      	params = {"TID":event.source.parent.getComponent('Label 5').text}
	      	
	      	system.db.runNamedQuery("Complete Task", params)
	      	
	else:
		print "oops!"

Add print statements after each variable to show their values. Perhaps it will shed some light on what's going on.

How do you know it isn't working on the first click? It writes to a tag and it causes a query to run. (An update query of some kind, presumable, as you are discarding the return value.) Are you monitoring the tag? Are you querying the database elsewhere to see the DB changes?

Such a script commonly included a system.db.refresh(....) operation to cause a table or graph to pick up the DB changes.

I am checking within the DB and with one click it does not update the database as well. I also have the query tied to the "data" component on the power table that updates every second for testing purposes. It does write to the memory tag on first click as well as display my print statements. Seems to always get stuck on the named query.

Here is my script as it stands now, I have added in print statements and teh system.db.refresh operation. This is still within the actionPerformed event.


	# Grab a reference to the table.
table = event.source.parent.getComponent('Power Table')
	 
selectedIndices = table.getSelectedRows()
	 
	# If the length of selectedIndices is greater than 0, then at least one row is selected.
if len(selectedIndices) > 0:
	     
	    for index in selectedIndices:
	     
	        taskId = table.data.getValueAt(index, "task_id1") 
	        print taskId  
	     
	        # We can do something with the value as we iterate, or append to a list and
	        # do something with the entire list after the for-loop completes.
	      	task = system.tag.write("[default]placeholder", taskId)
	     	print task
	    params = {"TID":event.source.parent.getComponent('Label 5').text}
	      	
	    system.db.runNamedQuery("Complete Task", params)
	      	
else:
		print "oops!"
		
system.db.refresh(table, "data")

I have added print statemtents now which all display correctly on first click. Seems to be getting caught up on the named query. Here is my script with the print and refresh operations.

# Grab a reference to the table.
table = event.source.parent.getComponent('Power Table')
	 
selectedIndices = table.getSelectedRows()
	 
	# If the length of selectedIndices is greater than 0, then at least one row is selected.
if len(selectedIndices) > 0:
	     
	    for index in selectedIndices:
	     
	        taskId = table.data.getValueAt(index, "task_id1") 
	        print taskId  
	     
	        # We can do something with the value as we iterate, or append to a list and
	        # do something with the entire list after the for-loop completes.
	      	task = system.tag.write("[default]placeholder", taskId)
	     	print task
	    params = {"TID":event.source.parent.getComponent('Label 5').text}
	      	
	    system.db.runNamedQuery("Complete Task", params)
	      	
else:
		print "oops!"
		
system.db.refresh(table, "data")

Are you sure you need a second click, or is it just that it actually needs some time to do what it's supposed to do ?

You're doing tag writes and queries in a loop, which might be a bad idea.

1 Like

Yes, I agree this isn't the best approach. Although, I was able to figure out a solution!

It seems rather than getting caught up on the named query, it may have something to do with the memory tag. I removed the tag write completely and used the component itself to grab the data I needed. This in turn allowed me to perform the script on a single click.

I may have been asking too much for the script to handle in a timely manner.