Scala #036: ComboBox.selection

中級篇

Java.use(better, src=Scala) # ソースコードの歩き方《中級篇》
ソースコードの歩き方《Scala2.8.0》

《著》小粒ちゃん@湘南組、小粒ちゃん@博多組《監修》小泉ひよ子とタマゴ倶楽部
第1版♪2010/07/14

Scala #035: TabbedPane.selection

》作業中です《

■ リスト項目を選択する


 29:   val petList = new ComboBox(petStrings) {
     ...
 31:     Command.listenTo(selection)
     ...
 33:   }

リスト項目を選択すると、Command が ComboBox.selection に呼応します。

 46:   object Command extends Publisher {
 47:     reactions += {
 48:       case SelectionChanged(source) =>
 49:         val cb = source.asInstanceOf[ComboBox[String]]
 50:         val petName = cb.selection.item.mkString
 51:         updateLabel(petName)
 52:     }
 53:   }

選択した項目が変化すると、イベント SelectionChanged が発生するので、それに反応する処理を記述します。

scala.swing.ComboBox
161	class ComboBox[A](items: Seq[A]) extends Component with Publisher {
162	  override lazy val peer: JComboBox = new JComboBox(ComboBox.newConstantModel(items)) with SuperMixin
163	 
164	  object selection extends Publisher {
165	    def index: Int = peer.getSelectedIndex
166	    def index_=(n: Int) { peer.setSelectedIndex(n) }
167	    def item: A = peer.getSelectedItem.asInstanceOf[A]
168	    def item_=(a: A) { peer.setSelectedItem(a) }
169	
170	    peer.addActionListener(Swing.ActionListener { e =>
171	      publish(event.SelectionChanged(ComboBox.this))
172	    })
173	  }


関連記事

TOP

Last updated♪2010/10/09