1: //..+....1....+....2....+....3....+....4....+....5....+....6....+....7....+....8
2: /*
3: * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
4: */
5: package components
6:
7: import java.awt.BorderLayout
8: import java.awt.Dimension
9: import java.awt.Font
10: import javax.swing.BorderFactory
11: import javax.swing.ImageIcon
12: import swing._
13: import swing.event._
14:
15: // ----------------------------------------
16: object ComboBoxDemo {
17: val version = ComboBoxDemo
18: .getClass.getName+": #1.0.13a"
19: }
20:
21: // ----------------------------------------
22: class ComboBoxDemo extends BorderPanel {
23: // implements ActionListener
24:
25: val petStrings = Array("Bird","Cat","Dog","Rabbit","Pig")
26:
27: // ---------------------------------------- Command::Invoker
28: val panel = this
29: val petList = new ComboBox(petStrings) {
30: selection.index = 4
31: Command.listenTo(selection)
32: panel.peer.add(peer, BorderLayout.PAGE_START)
33: }
34:
35: val picture = new Label {
36: horizontalAlignment = Alignment.Center
37: border = BorderFactory.createEmptyBorder(10,0,0,0)
38: preferredSize = new Dimension(177,122+10)
39: panel.peer.add(peer, BorderLayout.PAGE_END)
40: }
41: picture.font = picture.font.deriveFont(Font.ITALIC)
42:
43: updateLabel(petStrings(petList.selection.index))
44:
45: // ---------------------------------------- Command::Command
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: }
54:
55: def updateLabel(name: String) {
56: val icon = createImageIcon("images/%s.gif" format name)
57: picture.icon = icon
58: picture.tooltip = "A drawing of a %s" format name.toLowerCase
59: picture.text = if (icon == null) "Image not found" else null
60: }
61:
62: // ----------------------------------------
63: /** Returns an ImageIcon, or null if the path was invalid. */
64: def createImageIcon(path: String): ImageIcon = {
65: val imgURL = ComboBoxDemo.getClass.getResource(path)
66: if (imgURL != null) {
67: return new ImageIcon(imgURL)
68: } else {
69: System.err.println("Couldn't find file: " + path)
70: return null
71: }
72: }
73:
74: border = BorderFactory.createEmptyBorder(20,20,20,20)
75: }
76:
77: // ========================================