scala-swing icon indicating copy to clipboard operation
scala-swing copied to clipboard

scala.swing.Menu contents field permanently empty

Open scabug opened this issue 16 years ago • 2 comments

The following code prints 0, the length of the menu's contents field, but it should be 1. It is also impossible to access the elements in contents.

It is interesting to note, however, that if the menu was to be shown, it would indeed have the item "foobar" in it.

import scala.swing._
val menu = new Menu("File") {
  contents += new MenuItem("foobar")
}
println(menu.contents.length)

scabug avatar Sep 17 '09 14:09 scabug

Imported From: https://issues.scala-lang.org/browse/SI-2362?orig=1 Reporter: Tom Coxon (tomc)

scabug avatar Sep 17 '09 14:09 scabug

Kent Tong (freemant) said: Yeah, it is definitely incorrect. The core of the issue may be that JMenu is not really a JComponent container but Scala treats it so. To work around it, use the peer:

import scala.swing.Menu
import scala.swing.MenuItem

object Test extends App {
  val menu = new Menu("m1") {
    contents += new MenuItem("a")
  }
  
  println(menu.contents.size)
  println(menu.peer.getItemCount())
  
}

scabug avatar Aug 11 '13 09:08 scabug