JPopupMenu Fonts

Hi, I have an existing project that uses dynamic popup menu’s for users (system.gui.createPopupMenu()). This is working fine, however, we have recently gone to touch screens, and require font size modifications. If I collect the current font from the menu using .getFont() and manipulate it, then set it back in the PopupMenu using setFont(), it does not modify during runtime (or in designer). If I read back the new font size, it reads back correctly, but does not display it. I have modified my code below to show what I have tried.

Thank you.

names = []
funcs = []
publicFuncs = []
publicNames = []

publicNames.append(“TITLE”)
def load(event):
pass
publicFuncs.append(load)

names.append(“Public”)
funcs.append([publicNames, publicFuncs])
names.append(“USER 1”)
funcs.append([publicNames, publicFuncs])

from java.awt import Font
from javax.swing.plaf import FontUIResource

menu = system.gui.createPopupMenu(names, funcs)

#Menu Size Doesn’t seem to do anything. Tried 10 to 10000
menu.setSize(1000,1000)
originalFont = menu.getFont()
newFont1 = originalFont.deriveFont(24.0)
newFont2 = Font(‘Dialog’,0,36)
newFont3 = Font(Font.MONOSPACED, Font.BOLD, 36)
newFont4 = FontUIResource(‘Dialog’,0,36)
menu.setFont(newFont4)
readbackFont = menu.getFont()
print “New Font is: %s” % readbackFont
#Show event + X,Y Coordinates
menu.show(event,0,28)

Try recursing through the menu to set the font on every item. Also, when posting code or other plain text, use a line with just a triple-backquote (these ```) above and below the pasted text. That displays indentation and code formatting so we can understand…

Thanks for the code tip :slight_smile:

If I try to insert a recursive loop:

for item in menu:
	item.setFont(newFont4)

Java returns a error:
JyPopupMenu’ object is not iterable

Here is the original code with the formats:

names = []
funcs = []
publicFuncs = []
publicNames = []

publicNames.append("TITLE")
def load(event):
	pass
publicFuncs.append(load)
	
names.append("Public")
funcs.append([publicNames, publicFuncs])
names.append("USER 1")
funcs.append([publicNames, publicFuncs])
	
from java.awt import Font
from javax.swing.plaf import FontUIResource
	
menu = system.gui.createPopupMenu(names, funcs)

#Menu Size Doesn't seem to do anything. Tried 10 to 10000
menu.setSize(1000,1000)
originalFont = menu.getFont()
newFont1 = originalFont.deriveFont(24.0)
newFont2 = Font('Dialog',0,36)
newFont3 = Font(Font.MONOSPACED, Font.BOLD, 36)
newFont4 = FontUIResource('Dialog',0,36)
menu.setFont(newFont4)
readbackFont = menu.getFont()
print "New Font is: %s" % readbackFont	
#Define event + X,Y Coordinates
menu.show(event,0,28)

Try iterating over menu.subElements (https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/JPopupMenu.html#getSubElements()):

for item in menu.subElements:
	item.setFont(newFont4)

Thanks Paul! I guess I don’t understand the JPopupMenu as well as I thought I did.

I can try playing with the last issue, but I may as well go ahead and ask:

image

Any idea how to get the “TITLE” font to change? I tried a second sub-iteration on ‘item’, but alas, no go :frowning:

Got it, I used a format on the string setting the font size:

publicNames.append('<HTML><font size="7">TITLE</HTML>')

If there is another way, I’d be happy to find out…

Thanks again @PGriffith for the help!

You’ll need to recurse into the submenu items - the constructed JyPopupMenu itself contains JyMenuItems, which each have their own getSubMenus() method, and so on. A simple recursive function should do it, unless your menu gets over 1024 elements deep :smile: Something like this, after the createPopupMenu call:

menu = system.gui.createPopupMenu(names, funcs)

def updateFont(menu, font):
	menu.setFont(font)
	for item in menu.subElements:
		updateFont(item, font)

updateFont(menu, font4)
1 Like

2 posts were split to a new topic: Customizing Tag Browse Tree