1
+ /*
2
+ * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
3
+ *
4
+ * Redistribution and use in source and binary forms, with or without
5
+ * modification, are permitted provided that the following conditions
6
+ * are met:
7
+ *
8
+ * - Redistributions of source code must retain the above copyright
9
+ * notice, this list of conditions and the following disclaimer.
10
+ *
11
+ * - Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ *
15
+ * - Neither the name of Oracle or the names of its
16
+ * contributors may be used to endorse or promote products derived
17
+ * from this software without specific prior written permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+
32
+ package scala .swing .examples .tutorials .components
33
+
34
+ import java .awt .{Dimension , Font }
35
+ import javax .swing .ImageIcon
36
+
37
+ import scala .swing ._
38
+
39
+ /**
40
+ * Tutorial: How to Use Combo Boxes
41
+ * [[http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html ]]
42
+ *
43
+ * Source code reference:
44
+ * [[http://docs.oracle.com/javase/tutorial/uiswing/examples/components/CustomComboBoxDemoProject/src/components/CustomComboBoxDemo.java ]]
45
+ *
46
+ * CustomComboBoxDemo.scala uses the following files:
47
+ * /scala/swing/examples/tutorials/images/Bird.gif
48
+ * /scala/swing/examples/tutorials/images/Cat.gif
49
+ * /scala/swing/examples/tutorials/images/Dog.gif
50
+ * /scala/swing/examples/tutorials/images/Rabbit.gif
51
+ * /scala/swing/examples/tutorials/images/Pig.gif
52
+ */
53
+ class CustomComboBoxDemo extends BorderPanel {
54
+ val petStrings : Array [String ] = Array (" Bird" , " Cat" , " Dog" , " Rabbit" , " Pig" )
55
+ // val images: Array[Option[ImageIcon]] = new Array[Option[ImageIcon]](petStrings.length)
56
+ val intArray : Array [Int ] = (0 until petStrings.length ).toArray
57
+ /*
58
+ * Despite its use of EmptyBorder, this panel makes a fine content
59
+ * pane because the empty border just increases the panel's size
60
+ * and is "painted" on top of the panel's normal background. In
61
+ * other words, the JPanel fills its entire background if it's
62
+ * opaque (which it is by default); adding a border doesn't change
63
+ * that.
64
+ */
65
+
66
+ val images : Array [Option [ImageIcon ]] = petStrings.map( pet => {
67
+ val oImage = CustomComboBoxDemo .createImageIcon(s " /scala/swing/examples/tutorials/images/ $pet.gif " )
68
+ oImage.map( img => {img.setDescription(pet); img} )
69
+ })
70
+
71
+ // Create the combo box.
72
+ val petList = new ComboBox [Int ](intArray) {
73
+ renderer = new ComboBoxRenderer ()
74
+ preferredSize = new Dimension (200 , 130 )
75
+ maximumRowCount = 3
76
+ }
77
+
78
+ // Lay out the demo.
79
+ layout(petList) = BorderPanel .Position .Center
80
+ border = Swing .EmptyBorder (20 , 20 , 20 , 20 )
81
+
82
+ class ComboBoxRenderer extends ListView .AbstractRenderer [Int , Label ](new Label (" " )) {
83
+ var uhOhFont : Option [Font ] = None
84
+ /*
85
+ * This method finds the image and text corresponding
86
+ * to the selected value and returns the label, set up
87
+ * to display the text and image.
88
+ */
89
+ def configure ( listMe : ListView [_], isSelected : Boolean , cellHasFocus : Boolean , a : Int , index : Int ): Unit = {
90
+ // Set the icon and text. If icon was null, say so.
91
+ images(a) match {
92
+ case Some ( icon ) =>
93
+ component.icon = icon
94
+ component.text = petStrings(a)
95
+ component.font = listMe.font
96
+ case None => setUhOhText( s " ${petStrings(a)} (no image available) " , listMe.font)
97
+ }
98
+ }
99
+
100
+ // Set the font and text when no image was found.
101
+ def setUhOhText (uhOhText : String , normalFont : Font ): Unit = {
102
+ if (! uhOhFont.isDefined) { // lazily create this font
103
+ uhOhFont = Some (normalFont.deriveFont(Font .ITALIC ))
104
+ }
105
+ component.font = uhOhFont.getOrElse( normalFont )
106
+ component.text = uhOhText
107
+ }
108
+ }
109
+ }
110
+
111
+ object CustomComboBoxDemo extends SimpleSwingApplication {
112
+
113
+ def createImageIcon (path : String ): Option [javax.swing.ImageIcon ] =
114
+ Option (resourceFromClassloader(path)).map(imgURL => Swing .Icon (imgURL))
115
+
116
+ lazy val top = new MainFrame () {
117
+ title = " CustomComboBoxDemo"
118
+
119
+ // Create and set up the content pane.
120
+ val newContentPane = new CustomComboBoxDemo ()
121
+ newContentPane.opaque = true
122
+ contents = newContentPane
123
+ }
124
+
125
+ }
0 commit comments