Icon beside each item in popup menu

Hi!
I was wondering if it is possible to add icons on the left side of each item in the popup menu.
And how am I going to do this.

Thank you very much! :slight_smile:

You can do it by creating your popup menu with the underlying java classes instead of system.gui.createPopupMenu(). If you look at JMenuItem, you’ll see it has a constructor that takes an icon and a string. You can use this code to get you started:

def showPopup(event):
	if not event.popupTrigger:
		return
	from javax.swing import JPopupMenu, JMenuItem
	menu = JPopupMenu()
	x = JMenuItem("Show Tag Tree")
	def showTagTreeAction(event, comp=event.source.parent.getComponent("TagTree")):
		comp.visible = True
	x.addActionListener(showTagTreeAction)
	menu.insert(x, TagTreeIdx)
	menu.show(event.source, event.x, event.y)

Note that the show() method for a manually constructed JPopupMenu is different.

Hi!
Thank you for the reply. :slight_smile: I was able to create a sample menu. However, I cannot create the menu with the icon. :frowning: From the constructor, it says I can use JMenuItem(String text, Icon icon). I don’t know which property or component am I suppose to use for the icon. So I tried the following:

  1. Created an image and use its image path

icon = event.source.parent.getComponent('Image').path x = JMenuItem("test", icon)

  1. Use the image component itself

icon = event.source.parent.getComponent('Image').path x = JMenuItem(test, icon)

The error says the javax.swing.Icon has to be an integer. Sorry, I am new to scripting and I am clueless on how to represent icon into an integer type.

Thank you again!

The constructor I pointed out links to http://docs.oracle.com/javase/8/docs/api/javax/swing/Icon.html. Which shows the classes that implement Icon, including ImageIcon and various parts of MetalIconFactory. The latter includes many methods to get common UI icons. IIUC, Ignition doesn’t actually use Metal, so if you want them, you’ll have to dig around in Ignition’s chosen look-and-feel classes.
But ImageIcon() should create usable icons for you from local files or raw bytes.

1 Like

Hi! Thank you! I am able to do it now.
Thanks for your help. :thumb_left:

@pturmel I Need help with this set up that you recommended a while ago. i literally have no idea on how to do the java part on this jmenu, I opened a new topic also , but got no answers there, maybe you can guide me.

My goal is the following. I have a table with two columns in my db.
Table (Color,Name)


Color | customerName
Blue | ABD
Red | KB
Black | Libby
Green | Nestle

the idea is to create a JMenuItem with that information that will come from a query in my db.
i have use the following code that i found around in the forum (in a topic you also answered) in a Power Table where the menu works already but have not tried to put icon. I know this script won’t work with a JMenuItem, but it is just to show what i’m looking for.

def myScript(event,mybit):
	self.myscript(mybit)

query = "select  Color,  Customername from customers"
result = system.db.runQuery(query)
if len(result)>0:
	names = []
	functions=[]
	for row in result:
		print result
		id = row["Customername"]
		names.append("Customer: "+ str(id))
		functions.append(myScript)
	menu = system.gui.createPopupMenu(names, functions)
	menu.show(event)

but how can use something like that when creating my own JMenuItem? and also how can i add the ImageIcon to it.

I know it might be to much to ask for this explanation. and yes I’m also taking a couple Java Courses to get the hang on this language completely, but it would really help if i can get this code to run correctly with your help faster. the goal is to create a JMenuIItem with a Icon+String. Icon will be a square with background color that will be the one selected in the query.

that would be part 1. to create the menu, then once the user click on a selection of color and name, i will need to then use the selection to (UPDATE DB WITH THE SELECTED COLOR)

so any thought?

I Want something like that below

image