tags:

views:

306

answers:

1

How do I cause a popup to get shown in Scala? I have a "backdoor" but it seems pretty ugly to me:

val item = new MenuIterm(new Action("Say Hello") {
  def apply = println("Hello World");
})
//SO FAR SO GOOD, NOW FOR THE UGLY BIT!
val popup = new javax.swing.JPopupMenu
popup.add(item.peer)
popup.setVisible(true)
+1  A: 

What you are doing is fine, but if you'd like to hide the peer call you could create your own class:

class PopupMenu extends Component
{
  override lazy val peer : JPopupMenu = new JPopupMenu

  def add(item:MenuItem) : Unit = { peer.add(item.peer) }
  def setVisible(visible:Boolean) : Unit = { peer.setVisible(visible) }
  /* Create any other peer methods here */
}

Then you can use it like this:

val item = new MenuItem(new Action("Say Hello") {
  def apply = println("Hello World");
})

val popup = new PopupMenu
popup.add(item)
popup.setVisible(true)

As an alternative, you could try SQUIB (Scala's Quirky User Interface Builder). With SQUIB, the above code becomes:

popup(
  contents(
    menuitem(
      'text -> "Say Hello",
      actionPerformed(
        println("Hello World!")
      )
    )
  )
).setVisible(true)
dcstraw
Why is this (popup menu) not part of the standard scala swing toolkit? I must say that I've been a bit alarmed by the lack of documentation together with some pretty basic constructs. Makes me wonder whether anyone actually uses the toolkit.
oxbow_lakes