'Acessing parent object fields/methods in Clojure
I have a java.awt.Frame that is a descendent of java.awt.Component. I'm trying to get the peer field of the Component, or else call .getPeer on it.
(def f (new Frame "AWT test"))
(. f setSize 400 400)
(. f setLayout (new GridLayout 3 1))
(class f) ;;java.awt.Frame
(supers (class f)) ;; #{java.awt.Container java.io.Serializable java.awt.Window java.awt.image.ImageObserver java.awt.Component java.awt.MenuContainer java.lang.Object javax.accessibility.Accessible}
I can see that Component is a superclass, but can't figure out how to access it.
(filter #(instance? java.awt.Component %) (supers (class f))) ;; () - it returns empty
Yes, I know getPeers is deprecated. I'll likely need to do some reflection work after I get the Component. And I already have the requisite add-opens in play.
Solution 1:[1]
You just call (.getPeer f), like any other method. No fancy business is required to call a public method, whether declared in this class or in a superclass. Of course, this only works if you're using a version of Java old enough to support this method.
Solution 2:[2]
I still don't have a clear answer of how to filter by the class, but I did find a way to get the peer component:
(def acc (AWTAccessor/getComponentAccessor))
(.getPeer acc f);; #object[sun.awt.X11.XFramePeer 0x2fa3dc81 "sun.awt.X11.XFramePeer@2fa3dc81(7600007)"]
That's good enough for now.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | amalloy |
| Solution 2 | robbie.huffman |
