Skip to content

Instantly share code, notes, and snippets.

@kaizawa
Created February 11, 2011 16:06
Show Gist options
  • Save kaizawa/822555 to your computer and use it in GitHub Desktop.
Save kaizawa/822555 to your computer and use it in GitHub Desktop.
Sample code which listens to Enter key event on TextField
/**
* Sample code which listens to Enter key event on TextField
*/
package myscala
import scala.swing.BorderPanel
import scala.swing.Label
import scala.swing.MainFrame
import scala.swing.SimpleSwingApplication
import scala.swing.TextField
import scala.swing.event.Key
import scala.swing.event.KeyPressed
import BorderPanel.Position._
object Main extends SimpleSwingApplication {
/**
* Main Window
*/
def top = new MainFrame {
title = "TextFieldSample"
val textfield:TextField = new TextField("Input")
val label:Label = new Label("Output")
contents= new BorderPanel (){
add(textfield, North)
add(label, South)
}
// must specify 'keys' member of TextField, but not textfield it self.
listenTo(textfield.keys)
reactions += {
case KeyPressed(_, Key.Enter, _, _) => label.text_=(textfield.text)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment