The companion object for PopupMenu
doesn't serve any specific purpose here except functioning as a name space for the auxiliary trait JPopupMenuMixin
. This trait can then be "hidden" by making it private[PopupMenu]
, so it is only known by class PopupMenu
and its companion object.
Frankly, I don't see the purpose of that trait. It defines a method popupMenuWrapper
pointing to the outer Scala Swing component, but that method is not used at all. So a less confusing version would be simply:
import scala.swing._
import javax.swing.JPopupMenu
class PopupMenu extends Component with SequentialContainer.Wrapper {
override lazy val peer: JPopupMenu = new JPopupMenu with SuperMixin
def show(invoker: Component, x: Int, y: Int): Unit = peer.show(invoker.peer, x, y)
}
Test:
val pop = new PopupMenu {
contents += new MenuItem("Foo")
}
lazy val but: Button = Button("Test") {
pop.show(but, 0, 0)
}
val f = new Frame {
contents = but
pack().centerOnScreen()
open()
}
The only thing necessary for a wrapper is to extend scala.swing.Component
and override the peer
value with the underlying javax.swing
component. The mixin with SuperMixin
overrides a few methods of that component, such as paintComponent
, in order to forward them to the outer wrapper component. That's all.
The wrapper mixes in SequentialContainer.Wrapper
which allows for the contents +=
operation to add the menu items.