Add menu item to about menu

Is there a way to add a menu item into the Help menu so I can add my own “about my app” menu item?

Create an empty window in your project, name it something like Nav/Null, then add this to your Client Startup Script:

window = system.nav.openWindow("Nav/Null")
from javax.swing import SwingUtilities, JMenuItem
root = SwingUtilities.getRoot(window)
root.setResizable(False)
system.nav.closeWindow(window)
menu = root.getJMenuBar()

From menu you can use any of the methods of JMenuBar, eg: https://docs.oracle.com/javase/7/docs/api/javax/swing/JMenuBar.html#getMenu(int)

Once you have a reference to the ‘Help’ menu (probably, just use the overall child count) you can add a menu:

from functools import partial
def handleSelect(event, target):
	import system
	system.nav.swapTo(target)
menuItem = JMenuItem("About MyApplication", actionPerformed=partial(handleSelect, target="My Window Name"))
submenu.add(menuItem)
2 Likes

Thank you very much, it works great. However, I am having a lot of trouble trying to add an icon to the new menu element. I have tried these and I just cant make it work:

menuItem = JMenuItem("About MyApplication", actionPerformed=partial(handleSelect, target="My Window Name"))
menuItem.setIcon(ImageIcon(URL("http://mygateway:8088/gateway/images/Builtin/icons/16/information.png")))
submenu.add(menuItem)

Use PathIcon.

from functools import partial
from com.inductiveautomation.ignition.client.images import PathIcon
from javax.swing import JMenuItem

def handleSelect(event, target):
	import system
	w = system.nav.openWindow(target)
	system.nav.centerWindow(w)

submenu = project.util.getMenuBar()
helpMenu = submenu.getMenu(submenu.menuCount-1)

ico = PathIcon()
ico.path = "Builtin/icons/16/information.png"

menuItem = JMenuItem("About MyApplication", ico, actionPerformed=partial(handleSelect, target="My Window Name"))
menuItem.name = "custom"

if "custom" not in [menu.name for menu in helpMenu.menuComponents]:
	helpMenu.add(menuItem)

project.util

from javax.swing import SwingUtilities

_menu = None
_appContext = None

def getMenuBar():
	if project.util._menu is None:
		window = system.nav.openWindow("nav/null")
		root = SwingUtilities.getRoot(window)
		root.setResizable(False)
		system.nav.closeWindow(window)
		project.util._menu = root.getJMenuBar()
	return project.util._menu
	
def getAppContext():
	if project.util._appContext is None:
		window = system.nav.openWindow("nav/null")
		project.util._appContext = window.getAppContext()
		system.nav.closeWindow("nav/null")
	return project.util._appContext
	

3 Likes

Oh that’s perfect!.
Thank you both very much!

I came across this thread, and thanks to the script examples I and was able to implement custom menu items. However, this is a windowed application and I have noticed that my maximize button becomes disabled when I call the scripts to add to my menu bar. Some googling seems like this could be a java bug so I’m curious if anyone has a suggestion on how to restore the maximize function? Or prevent the disable from happening in the first place?

image

Just comment out the line:

and it will work well again.

1 Like

#facepalm, thanks!

1 Like

This post is quite old but I have a similar need so I tried to implement the solution proposed by @PGriffith and @Kyle_Chase and I found that it works great, unless you’re using a localized application, as I am: actually you can add the menu where you want but when you change language it disappears.
From further investigation I discovered that it’s only hidden: if I log off and back in I have it available again. That makes me think of a problem updating the menu tree:
is there a way to inform the JMenuBar about its new item(s) so it can update them when I change language.

Finally I got it working, but I had to redefine the util module as follows:

from javax.swing import SwingUtilities

_appContext = None
def getMenuBar():
	return getAppContext().getRootPaneContainer().getJMenuBar()
	
def getAppContext():
	if shared.menu.util._appContext is None:
		window = system.nav.openWindow("nav/null")		
		shared.menu.util._appContext = window.getAppContext()
		system.nav.closeWindow(window)
	return shared.menu.util._appContext

Seems like the JMenuBar has different “handler” from call to call

1 Like