Java.use(better, Scala); tips343_colorList

《前の記事|記事一覧|次の記事》
Java.use(better, Scala)


Tips #343

ListView



私たちの人生は
私たちが費やした努力だけの価値がある
François Mauriac - Wikipedia

《関連記事》

-


┃アプリケーションの動作



(左)リスト項目を選択すると(右)パネルの色が変化します。

  • 選択したリスト項目 blue に呼応して、パネルの背景色を再設定します。
■ AppWindow.scala

    
tips343_colorList/scala/src/AppWindow.scala
1: /* 2: * Copyright (c) 2010-2014, KOTSUBU-chan and/or its affiliates. 3: * Copyright (c) 1998, Atelier-AYA. 4: * All rights reserved. 5: */ 6: package cherry.pie 7: 8: // ---------------------------------------- 9: import swing._ 10: object appWindow extends MainFrame { 11: val version = appWindow 12: .getClass.getName+": #1.0.04" 13: // ---------------------------------------- 14: title = "ListView" 15: contents = TopPanel 16: peer.setLocationRelativeTo(null) 17: } 18: 19: // ---------------------------------------- 20: object TopPanel extends FlowPanel { 21: _self => 22: val controlPane = new ScrollPane { 23: preferredSize = new Dimension(100,100) 24: contents = new ListView[String](ColorMap.keys) { 25: listenTo(selection) 26: import swing.event.ListSelectionChanged 27: reactions += { 28: case ListSelectionChanged( 29: source: ListView[String], range, live) => 30: val key = source.selection.items(0) 31: canvasPane.background = ColorMap.color(key) 32: } 33: } 34: } 35: val canvasPane = new Panel { 36: preferredSize = new Dimension(100,100) 37: } 38: new SplitPane { 39: _self.contents += this 40: contents_=(controlPane, canvasPane) 41: // leftComponent = controlPane 42: // rightComponent = canvasPane 43: orientation = Orientation.Vertical 44: dividerLocation = 100 45: } 46: } 47: 48: // ---------------------------------------- 49: object ColorMap { 50: import java.awt.Color 51: val colorChart = Map( 52: "red" -> Color.red, 53: "green" -> Color.green, 54: "blue" -> Color.blue, 55: "cyan" -> Color.cyan, 56: "magenta" -> Color.magenta, 57: "yellow" -> Color.yellow, 58: "black" -> Color.black, 59: "white" -> Color.white) 60: def keys = colorChart.keys.toList sortWith { _

ListView

[24]ListView を利用して、リスト項目を管理します。

■ ListSelectionChanged

[28]発生したイベント ListSelectionChanged を介して

  • [29]選択したリスト項目 source にアクセスできます。

TOP
》作業中です《

Created: 2010/01/29|Last updated: 2014/01/01 17:12:06