# JMenuItem Antialias

**URL:** <https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [August 19, 2019, 9:14am UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556 "2019-08-19T09:14:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 19, 2019, 9:14am UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/1 "2019-08-19T09:14:41Z")

</div>

How can I set antialias for texts in my right-click menu (JMenuItem):  
 ![JMenuItem_Antialias](https://us1.discourse-cdn.com/inductiveautomation/original/2X/1/170721eb412cf0c236b1d4bdde1f8e27ffc28de7.png)

```auto
	from functools import partial 
	from javax.swing import JMenuItem
	from javax.swing.border import EmptyBorder
	from com.inductiveautomation.ignition.client.images import PathIcon
	from java.awt import Color
	from java.awt import Font

	menu = system.gui.createPopupMenu([],[])
	font_Size = 18
	font = Font("Arial Narrow", Font.BOLD,font_Size)
	ico = PathIcon()
	ico.path = "checkmark_blue_24.png"
	menuitem3 = JMenuItem (menuText3,ico, actionPerformed=partial(project.popmenu.setConveyorManualOFF_ON,objekt=event))
	menuitem3.setForeground(Color.BLACK)
	
	#menuitem3.setFont(menuitem3.getFont().deriveFont(fontSize))
	#menuitem3.setFont(Font("Arial Narrow", Font.BOLD,font_Size))
	menuitem3.setFont(font)
	
	if "Operater" in system.security.getRoles():
		menuitem3.setEnabled(True)
	else:
		menuitem3.setEnabled(False)
	menu.add(menuitem3)
	

```

All other texts are antialiased, only right-click menu looks terrible…  
Oh, this is on Windows 10, with no scaling (100%).

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 19, 2019, 11:18am UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/2 "2019-08-19T11:18:13Z")

</div>

JMenuItems are subclasses of [JComponent](https://docs.oracle.com/javase/8/docs/api/javax/swing/JMenuItem.html). Consider further subclassing them and re-implement paintComponent() to turn on the [anti-aliasing rendering hint](https://docs.oracle.com/javase/tutorial/2d/text/renderinghints.html) before calling the superclass method.

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 19, 2019, 12:25pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/3 "2019-08-19T12:25:00Z")

</div>

While I appreciate your answer, I don’t quite understand it… 😳  
I’m sorry, but can you provide a little sample code, please?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 19, 2019, 1:35pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/4 "2019-08-19T13:35:02Z")

</div>

In some project script (project.popmenu, perhaps), have something like this:

```auto
from java.awt import RenderingHints
from javax.swing import JMenuItem

class antiAliasedJMI(JMenuItem):
    def __init__ (self, txt, ico, action):
        super(JMenuItem, self). __init__ (txt, ico, actionPerformed=action)
    def paintComponent(self, g):
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
        super(JMenuItem, self).paintComponent(g)

```

Then use this class ( project.popmenu.antiAliasedJMI ) instead of JMenuItem wherever you need anti-aliasing.

(Untested…)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 19, 2019, 3:40pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/5 "2019-08-19T15:40:54Z")

</div>

It won’t help you in 7.9, but I also just filed a ticket for us to do this automatically in 8.0, since we’re anti-aliasing everywhere.

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 19, 2019, 4:00pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/6 "2019-08-19T16:00:37Z")

</div>

This is what I get:

```auto
Exception in thread "AWT-EventQueue-0" Traceback (most recent call last):
  File "<module:project.popmenu>", line 9, in paintComponent
AttributeError: 'super' object has no attribute 'paintComponent'

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 19, 2019, 7:58pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/7 "2019-08-19T19:58:56Z")

</div>

Try without the explicit class and object, just for paintComponent. Like so:

```auto
        super().paintComponent(g)

```

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 20, 2019, 5:33am UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/8 "2019-08-20T05:33:15Z")

</div>

If I try super().paintComponent(g), I get:

```auto
Exception in thread "AWT-EventQueue-0" Traceback (most recent call last):
  File "<module:project.popmenu>", line 9, in paintComponent
TypeError: super() takes 1-2 arguments (0 given)

```

I also tried:  
super.paintComponent(g)  
super(JMenuItem).paintComponent(g)  
super(self).paintComponent(g)  
and I always get:

```auto
Exception in thread "AWT-EventQueue-0" Traceback (most recent call last):
  File "<module:project.popmenu>", line 9, in paintComponent
AttributeError: type object 'super' has no attribute 'paintComponent'

```

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 20, 2019, 7:28am UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/9 "2019-08-20T07:28:10Z")

</div>

If I change the paintComponent(g) to paintComponents(g) then the code works.

```auto
from java.awt import RenderingHints
from java.awt import Toolkit
from java.util import Map
from javax.swing import JMenuItem

class antiAliasedJMI(JMenuItem):
	def __init__ (self, txt, ico, action):
		super(JMenuItem, self). __init__ (txt, ico, actionPerformed=action)
	def paintComponents(self, g):
		#desktopHints = Map(Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints"))
		#print "desktopHints= ", desktopHints
		#g.setRenderingHint(desktopHints)
		g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,	RenderingHints.VALUE_ANTIALIAS_ON)
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
		g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON)
		#super(JMenuItem, self).paintComponents(g)
		super.paintComponents(g)
		#repaint(g)

```

But the antialiasing is not working… text still look… not good…

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 20, 2019, 11:24am UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/10 "2019-08-20T11:24:15Z")

</div>

I suspect that adding the “s” simply makes your method not be called at all, as it isn’t overriding anything that the GUI uses. Consider my original suggestion, but replacing JMenuItem with its parent classes until you find one that works. Possibly JComponent.

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 20, 2019, 11:56am UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/11 "2019-08-20T11:56:18Z")

</div>

Well… replacing JMenuItem with JComponent also doesn’t work…  
And I really don’t understand this class stuff…  
so I guess there is no antialiasing for me…

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 20, 2019, 5:31pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/12 "2019-08-20T17:31:22Z")

</div>

paintComponent() is a protected method of JComponent so it can not be called from a subclass.

JMenuItem also inherits from JContainer (because JComponent is a subclass of JContainer) which includes the public method paintComponents which is why when you add the “s” your code does not error, but also does not yeild the expected result.

A way to check this is to run the following script which will return all of the callable attributes and methods of the class:

```auto
from javax.swing import JMenuItem

print dir(JMenuItem)

```

If you take the time to read all of that, you will find that paintComponent is not listed, but paintComponents is. This is because jython is honoring the Java Accessibility.

So, if you want antialiasing then you will need to paint the component yourself without calling the super. That would be fairly involved, but could be done.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 20, 2019, 5:35pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/13 "2019-08-20T17:35:27Z")

</div>

> [@lrose](#):
>
> paintComponent() is a protected method of JComponent so it can not be called from a subclass.

This is wrong. The entire purpose of `protected` visibility is to allow subclasses access without allowing outside access. This is perhaps why you don't see it listed with `dir`, because given an instance of a class you can't call protected methods or access protected variables, but you can certainly access or override from a subclass.

---

<div class="post-metadata">

**Author:** ![zxcslo](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/zxcslo/32/61610_2.png) [@zxcslo](https://forum.inductiveautomation.com/u/zxcslo)\
**Post date:** [August 20, 2019, 5:38pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/14 "2019-08-20T17:38:12Z")

</div>

So @Kevin.Herron how can then this be done… antialiasing on JMenuItem I mean?

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 20, 2019, 5:44pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/15 "2019-08-20T17:44:34Z")

</div>

I don’t know, thought @pturmel’s approach would have worked assuming the syntax is correct.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 20, 2019, 5:56pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/16 "2019-08-20T17:56:34Z")

</div>

There’s a bug within the Jython interpreter. You can use the automatically-created `super__` method to directly call the super method; `super()` _should_ work but doesn’t:

```auto
from java.awt import RenderingHints
from java.awt import Toolkit
from java.util import Map
from javax.swing import JMenuItem, JComponent

from functools import partial

class antiAliasedJMI(JMenuItem):
	def paintComponent(self, g):
		g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,	RenderingHints.VALUE_ANTIALIAS_ON)
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
		g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON)
		self.super__paintComponent(g)

a = antiAliasedJMI("text", None, action=partial(system.gui.messageBox, "title", "message"))

menu = system.gui.createPopupMenu([],[])

menu.add(a)

menu.show(event)

```

Also, I don’t think you need to override **init** , which makes it all feel a _little_ less “magic”.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 20, 2019, 6:03pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/17 "2019-08-20T18:03:24Z")

</div>

Dangit, I was just about to post this 🙂

the `super(Type, self)` syntax doesn’t work with protected methods in Jython for whatever reason.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 20, 2019, 6:10pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/18 "2019-08-20T18:10:16Z")

</div>

Well, my ignorance is showing again.

I’ll put this on the list of lesson’s learned.

errrr…I really dislike being wrong

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [August 20, 2019, 6:31pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/19 "2019-08-20T18:31:00Z")

</div>

Everything you said would have been correct for `private` visibility. Maybe you just mixed them up.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [August 20, 2019, 6:48pm UTC](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556/20 "2019-08-20T18:48:31Z")

</div>

As much as I would love to claim I had just mixed them up, I won’t.

I wrote what I thought to be correct. However, I will take it as the learning it is, as I have had plenty of occasions where I needed to call protected super methods, and now I know the ‘trick’ to accomplishing that.

I do dislike being wrong, but it really is one of the best ways to learn.

[Next page](https://forum.inductiveautomation.com/t/jmenuitem-antialias/27556.md?page=2)
