Get references of buttons that are already created in other plugins in JAVA

Hi folks,

I am designing a script in JAVA and I would like to get the reference of a button which belongs of a frame of another plugin. e.g:“Threshold…”) executes the Threshold Window but when I click in the “Apply button” Record Window does not return a reference belonging to “Apply Button” or “Convert to Mask”. I need this button reference to create a ActionListener event to execute other functions (only when I click in the Apply button). As you can see, Threshold Window is not a class itself, but a plugin. Also, I would like to click the reference of “Convert to Mask Button” belonging to Thresholder window which is activated when “Apply button” is clicked. In short, get references of buttons that are already created in other plugins



Hello Luis -

For your example case of the “Threshold” dialog, you can use the
WindowManager to get a reference to the “Threshold” window.
Then get the window’s applyB property.

In the case of the “Theshold” dialog, the actual window is an
instance of the ThresholdAdjuster class. It’s applyB
button is package-private, so you will either have to use
reflection to actually access it, or write a helper class that you
put into ThresholdAdjuster's package, viz., ij.plugin.frame.

Here is a jython script that uses reflection for this:

from ij import IJ
from ij import WindowManager

print  IJ.getFullVersion()

# open up "Threshold" window
imp = IJ.createImage ('8-bit ramp', '8-bit ramp', 256, 256, 1) ('Threshold...')

# get (reference to) "Threshold" window
thr = WindowManager.getWindow ('Threshold')
print  type (thr)

# use reflection to make "ThresholdAdjuster.applyB" accessible
f = thr.getClass().getDeclaredField ('applyB')
f.setAccessible (True)

# get reference to "applyB"
b = f.get (thr)
print  b
b.setLabel ('Hello Luis!')
thr.setVisible (True)

And here is its output:

<type 'ij.plugin.frame.ThresholdAdjuster'>

Thanks, mm

1 Like

Hi @mountain_man,

My apologies for taking too long in answer you.

Thanks a lot for your helping and good quarantine.

All the best.