[英]Changing Scala Swing ComboBox Items
I tried to implement a date selection using three ComboBox as shown below. 我尝试使用三个ComboBox实现日期选择,如下所示。
contents += new Label("Selected Date:")
val dayBox = new ComboBox(1 to 31)
contents += dayBox
val monthBox = new ComboBox(List("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
contents += monthBox
listenTo(monthBox.selection)
reactions += {
case SelectionChanged(`monthBox`) => Dialog.showMessage(ui, "Month changed.")
}
contents += new ComboBox(2011 to 2020)
How can I change the items in dayBox as I change the monthBox? 当我更改monthBox时,如何更改dayBox中的项目? I knew that the Dialog.showMessage part should be changed. 我知道应该更改Dialog.showMessage部分。 But I am wondering how? 但我想知道怎么样?
For example, when I change to Jan, Mar, May, Jul, Aug, Oct, Dec, the day should show 31, while 30 for others except 28 for Feb. 例如,当我改为1月,3月,5月,7月,8月,10月,12月时,那天应该显示31,而其他30则除了2月28日。
You have to use the peer JComboBox
object of the ComboBox
to change the items in it: 您必须使用ComboBox
的对等JComboBox
对象来更改其中的项目:
scala> import swing.ComboBox
import swing.ComboBox
scala> val cb = new ComboBox(1 to 31)
cb: scala.swing.ComboBox[Int] = scala.swing wrapper scala.swing.ComboBox$$anon$1[...]
scala> cb.peer.getModel.getSize
res6: Int = 31
scala> cb.peer.setModel(ComboBox.newConstantModel(1 to 30))
scala> cb.peer.getModel.getSize
res8: Int = 30
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.