Returning lists of arguments for bound methods

Hi, this is a follow on and generalisation from the discussion at Power Table header height

If I want to interrogate a Vision component and find out how to do things with it that aren't documented, very often it's useful to use dir() to return a list of attributes. Then with a bit of trial and error, either writing to properties directly or using a bound method lets me do what I want to do.

But I run into a roadblock when dealing with bound methods without obvious arguments.

As an example, if I want to use code similar to that in the linked post, but change the header font of a table, I go:

from java.awt import
Dimension table = self.getTable()
header = table.getTableHeader()
header.setFont( ???? )
header.revalidate()

But I have no idea in advance what arguments to use for header.setFont.
What are the arguments for setFont?

And, more importantly,
How do you return a list of arguments for a bound method like this?

Kind regards,
Sean

This might be useful:
https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.1/index.html

Although the v7 one was better for navigating around:
http://files.inductiveautomation.com/sdk/javadoc/ignition79/795/index.html

setFont requires a Java.awt.Font object. If you just want to change an attribute of the current font, you can use getFont() to get the current font and then manip that.

PS to find those two APIs, just search “Ignition API” in Google and you’ll find the SDK guide which has those links there

1 Like

Hi Nick, thank you so much, those were really helpful.

I managed to use the constructor detail at the Java.awt.Font object page to end up with

header.setFont("Dialog",1,24)
which gave me what I wanted in this case.

Just a few questions, if you feel like them:

  1. Is there a way to actually return the arguments directly without using external documentation? (Guessing no given your answer but asking anyway).

  2. The reason I couldn't figure this one out was that when I printed out the results from header.getFont, it returned this:

java.awt.Font[family=Dialog,name=Dialog,style=bold,size=20]

I "followed my nose" and quickly found that setFont doesn't allow you to use keywords, etc, etc, so I couldn't find a way to manipulate it easily.

So as a general practice, was the way that worked for me this time - namely, going to the java documentation and looking for the constructor for that object - what I should do when I'm trying to define object in general?

  1. Is it OK to just import java.awt (or even java), or should I just import the object I need, in this case java.awt.Font? What sort of impact does it have on performance to import the whole .awt library?

Thanks once more for your help, I have no software background but am self teaching, and a little assistance sometimes saves a lot of heartache.

Most likely not:

Yes, the java docs will tell you how to use the objects you're looking at using. Can't drive without being taught how the clutch works! (and no, automatic cars aren't real cars)

Not sure of impacts I presume it would increase memory usage and possibly increase load time, but I would suggest not importing things you don't need; only import what you need.

1 Like

Y'all might find this topic helpful:

3 Likes

Nope. Technically JVM bytecode does allow for named method arguments, but in practice no Java compiler retains that information (other JVM languages can, though), so there's nothing to reconstruct at runtime.

Yes, this is an unfortunate clash with the style of Python and Java. Jython allows you to decorate a Java method specially to allow for keyword invocation, which we try to do for our scripting functions, but we can't decorate classes outside of our codebase like this.

The short answer is that it depends. When working in Vision, most things you interact with are either directly or indirectly instances of some Java Swing base class, so building up a working knowledge of Swing (and its predecessor, AWT, whose Font and Color and various other classes are still used) is useful.

Jython's import system is complicated and somewhat fraught. I always use direct, fully-qualified references, so:

Good:

from java.awt import Font
from java.awt import Font, Color

Bad:

import java.awt
from java.awt import *

It's more verbose, which is a shame, but it's very explicit about what you're importing (good for future maintenance) and has never caused me any headaches with stale imports or other weird problems.

Also, Phil's introspect.py is amazingly useful, because it retrieves typed method arguments in a way that dir cannot.

3 Likes

Hi, just wanted to say a big thank you to all three of you @nminchin, @pturmel, @PGriffith. This was really helpful for me. I’m a lurker in these forums and it was pretty cool to have the equivalent of an all-star team helping me out here.

Phil, I’ll try out introspect next time I run into a similar case.

EDIT: Added concurrence that autos are indeed not real cars.