summaryrefslogtreecommitdiff
path: root/docs/examples/swing/ComboBoxes.scala
diff options
context:
space:
mode:
authorSimon Ochsenreither <simon@ochsenreither.de>2011-12-07 21:52:20 +0100
committerSimon Ochsenreither <simon@ochsenreither.de>2011-12-07 22:09:04 +0100
commitf3e7e98cc0f47eacb59400b2c56c6bd2cb8b2ebc (patch)
tree6d77a6dbbb3de69bc38a7e2d55a37596960675a0 /docs/examples/swing/ComboBoxes.scala
parent332fec96e31840878bed41dd7b5314b97d8da7c2 (diff)
downloadscala-f3e7e98cc0f47eacb59400b2c56c6bd2cb8b2ebc.tar.gz
scala-f3e7e98cc0f47eacb59400b2c56c6bd2cb8b2ebc.tar.bz2
scala-f3e7e98cc0f47eacb59400b2c56c6bd2cb8b2ebc.zip
Clean up standard/swing library by deprecating/moving code examples
Deprecate scala/xml/include/sax/Main.scala. Move scala/swing/test/* to docs/examples. Saves 160KB in scala-swing.jar. Fixes SI-4627.
Diffstat (limited to 'docs/examples/swing/ComboBoxes.scala')
-rw-r--r--docs/examples/swing/ComboBoxes.scala86
1 files changed, 86 insertions, 0 deletions
diff --git a/docs/examples/swing/ComboBoxes.scala b/docs/examples/swing/ComboBoxes.scala
new file mode 100644
index 0000000000..c6ee19013a
--- /dev/null
+++ b/docs/examples/swing/ComboBoxes.scala
@@ -0,0 +1,86 @@
+package examples.swing
+
+import swing._
+import event._
+import java.util.Date
+import java.awt.Color
+import java.text.SimpleDateFormat
+import javax.swing.{Icon, ImageIcon}
+
+/**
+ * Demonstrates how to use combo boxes and custom item renderers.
+ *
+ * TODO: clean up layout
+ */
+object ComboBoxes extends SimpleSwingApplication {
+ import ComboBox._
+ lazy val ui = new FlowPanel {
+ contents += new ComboBox(List(1,2,3,4))
+
+ val patterns = List("dd MMMMM yyyy",
+ "dd.MM.yy",
+ "MM/dd/yy",
+ "yyyy.MM.dd G 'at' hh:mm:ss z",
+ "EEE, MMM d, ''yy",
+ "h:mm a",
+ "H:mm:ss:SSS",
+ "K:mm a,z",
+ "yyyy.MMMMM.dd GGG hh:mm aaa")
+ val dateBox = new ComboBox(patterns) { makeEditable() }
+ contents += dateBox
+ val field = new TextField(20) { editable = false }
+ contents += field
+
+ reactions += {
+ case SelectionChanged(`dateBox`) => reformat()
+ }
+ listenTo(dateBox.selection)
+
+ def reformat() {
+ try {
+ val today = new Date
+ val formatter = new SimpleDateFormat(dateBox.selection.item)
+ val dateString = formatter.format(today)
+ field.foreground = Color.black
+ field.text = dateString
+ } catch {
+ case e: IllegalArgumentException =>
+ field.foreground = Color.red
+ field.text = "Error: " + e.getMessage
+ }
+ }
+
+
+ val icons = try {
+ List(new ImageIcon(resourceFromClassloader("images/margarita1.jpg")),
+ new ImageIcon(resourceFromClassloader("images/margarita2.jpg")),
+ new ImageIcon(resourceFromClassloader("images/rose.jpg")),
+ new ImageIcon(resourceFromClassloader("images/banana.jpg")))
+ } catch {
+ case _ =>
+ println("Couldn't load images for combo box")
+ List(Swing.EmptyIcon)
+ }
+
+ val iconBox = new ComboBox(icons) {
+ renderer = new ListView.AbstractRenderer[Icon, Label](new Label) {
+ def configure(list: ListView[_], isSelected: Boolean, focused: Boolean, icon: Icon, index: Int) {
+ component.icon = icon
+ component.xAlignment = Alignment.Center
+ if(isSelected) {
+ component.border = Swing.LineBorder(list.selectionBackground, 3)
+ } else {
+ component.border = Swing.EmptyBorder(3)
+ }
+ }
+ }
+ }
+ contents += iconBox
+ }
+
+ def top = new MainFrame {
+ title = "ComboBoxes Demo"
+ contents = ui
+ }
+}
+