SlideShare a Scribd company logo
Scala Swing Ken Scambler
Origins of Swing  Java’s main desktop programming environment
Single Threaded, MVC framework
Developed from Abstract Windowing Toolkit (AWT) in the late 90s
Sun has focused on the web rather than desktop
Has faded from the limelight
However still has many users and devotees
Swing’s Advantages Cross-platform (mostly!)
Extremely powerful
Flexible
Extensible
Uses hardware acceleration
Comparable to native performance (nowadays!)
Swing’s Disadvantages Huge, complex API
Requires very verbose code
Events require anonymous inner class gymnastics!
Doesn’t integrate with Collection API
Models aren’t typesafe
Introducing Scala Swing Thin wrapper framework
Far less code
Dramatically simpler API
Full integration with Scala Collections
Introducing Scala Swing Scala getter/setter properties
Events use pattern-matching and partial functions
Performance is just as good
Panels & Layouts Layouts are built into the panel

More Related Content

DOCX
Last speech of tlatoani cuauhtemoc (1)
PDF
manual preparaciones LMA, empresa prestadora de servicios alimentarios de Junaeb
PPTX
Online grocery shopping survey results
PDF
Spring camp 발표자료
PDF
Vagrant와 chef로 개발서버 구축 자동화하기
PDF
스프링 시큐리티 구조 이해
PDF
[H3 2012] 내컴에선 잘되던데? - vagrant로 서버와 동일한 개발환경 꾸미기
ODP
Scala 2 + 2 > 4
Last speech of tlatoani cuauhtemoc (1)
manual preparaciones LMA, empresa prestadora de servicios alimentarios de Junaeb
Online grocery shopping survey results
Spring camp 발표자료
Vagrant와 chef로 개발서버 구축 자동화하기
스프링 시큐리티 구조 이해
[H3 2012] 내컴에선 잘되던데? - vagrant로 서버와 동일한 개발환경 꾸미기
Scala 2 + 2 > 4

Similar to Scala+swing (20)

PPT
Svcc Building Rich Applications with Groovy's SwingBuilder
PDF
Jetpack Compose - A Lightning Tour
PPT
PPTX
JAVA AWT
PDF
mobl: Een DSL voor mobiele applicatieontwikkeling
PPTX
Javascript And J Query
PPTX
PPT
SDC - Einführung in Scala
PDF
mobl presentation @ IHomer
PDF
Java ProgrammingImplement an auction application with the followin.pdf
KEY
Gwt and Xtend
PPTX
Flink Forward San Francisco 2019: Build a Table-centric Apache Flink Ecosyste...
PPTX
Flink Forward San Francisco 2019: Build a Table-centric Apache Flink Ecosyste...
PDF
Computer Graphics in Java and Scala - Part 1b
PDF
mobl
PDF
Asynchronous Programming at Netflix
PPT
CodeMash - Building Rich Apps with Groovy SwingBuilder
PDF
Kotlin, why?
PPT
JBUG 11 - Scala For Java Programmers
PDF
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Svcc Building Rich Applications with Groovy's SwingBuilder
Jetpack Compose - A Lightning Tour
JAVA AWT
mobl: Een DSL voor mobiele applicatieontwikkeling
Javascript And J Query
SDC - Einführung in Scala
mobl presentation @ IHomer
Java ProgrammingImplement an auction application with the followin.pdf
Gwt and Xtend
Flink Forward San Francisco 2019: Build a Table-centric Apache Flink Ecosyste...
Flink Forward San Francisco 2019: Build a Table-centric Apache Flink Ecosyste...
Computer Graphics in Java and Scala - Part 1b
mobl
Asynchronous Programming at Netflix
CodeMash - Building Rich Apps with Groovy SwingBuilder
Kotlin, why?
JBUG 11 - Scala For Java Programmers
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Ad

Recently uploaded (20)

PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Mushroom cultivation and it's methods.pdf
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
Teaching material agriculture food technology
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Getting Started with Data Integration: FME Form 101
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
A comparative study of natural language inference in Swahili using monolingua...
Advanced methodologies resolving dimensionality complications for autism neur...
MIND Revenue Release Quarter 2 2025 Press Release
SOPHOS-XG Firewall Administrator PPT.pptx
Empathic Computing: Creating Shared Understanding
Mushroom cultivation and it's methods.pdf
Univ-Connecticut-ChatGPT-Presentaion.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation_ Review paper, used for researhc scholars
A comparative analysis of optical character recognition models for extracting...
Building Integrated photovoltaic BIPV_UPV.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Teaching material agriculture food technology
Group 1 Presentation -Planning and Decision Making .pptx
Getting Started with Data Integration: FME Form 101
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Weekly Chronicles - August'25-Week II
TLE Review Electricity (Electricity).pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Ad

Scala+swing

  • 1. Scala Swing Ken Scambler
  • 2. Origins of Swing Java’s main desktop programming environment
  • 4. Developed from Abstract Windowing Toolkit (AWT) in the late 90s
  • 5. Sun has focused on the web rather than desktop
  • 6. Has faded from the limelight
  • 7. However still has many users and devotees
  • 13. Comparable to native performance (nowadays!)
  • 16. Events require anonymous inner class gymnastics!
  • 17. Doesn’t integrate with Collection API
  • 19. Introducing Scala Swing Thin wrapper framework
  • 22. Full integration with Scala Collections
  • 23. Introducing Scala Swing Scala getter/setter properties
  • 24. Events use pattern-matching and partial functions
  • 26. Panels & Layouts Layouts are built into the panel
  • 28. Add components with 'contents' buffer
  • 29. FlowPanel new FlowPanel { contents += new Button( "A" ) contents += new Button( "B" ) contents += new Button( "C" ) contents += new Button( "D" ) }
  • 30. GridPanel new GridPanel(3,3) { contents ++= 1 to 9 map ( n => new Button(n.toString)) }
  • 31. BorderPanel new BorderPanel { import BorderPanel.Position._ layout( new Button( "West" )) = West layout( new Button( "Center" )) = Center layout( new Button( "North" )) = North }
  • 32. Events No more XXXListeners!
  • 33. Individual events are case classes
  • 34. Event handlers are PartialFunctions which pattern match on events
  • 35. Classes which emit events have the Publisher trait
  • 36. Classes which listen to Publishers have the Reactor trait.
  • 37. Publishers & Reactors trait Reactor { val reactions: Reactions def listenTo(ps: Publisher*): Unit def deafTo(ps: Publisher*): Unit } trait Publisher extends Reactor { def publish(e: Event): Unit listenTo( this ) }
  • 38. ButtonClicked events new FlowPanel { // Panels are Reactors // Buttons are Publishers val button = new Button( "abc" ) listenTo(button) reactions += { case ButtonClicked(b) => println( "Clicked " + b.text) } //... }
  • 39. ButtonClicked events // All publishers are reactors // that listen to themselves new Button( "abc" ) { reactions += { case ButtonClicked(b) => println( "Clicked " + b.text) } }
  • 40. Mouse Events new FlowPanel { val pub = new FlowPanel listenTo( pub .mouse.clicks, pub .mouse.moves, pub .mouse.wheel) reactions += { case me: MouseClicked => case me: MouseDragged => case me: MouseEntered => case me: MouseExited => case me: MouseMoved => case me: MouseWheelMoved => } }
  • 41. Selection Events new FlowPanel { val fruitList = List( "apples", "bananas", "pears" ) val listView = new ListView(fruitList ) listenTo( listView .selection) reactions += { case SelectionChanged(`fruitList`) => } }
  • 42. ListViews class ListView[A] extends Component { def this (items: Seq[A]) = //... //... }
  • 43. ListViews case class City(name: String, country: String, population: Int , capital: Boolean ) val items = List( City( "Lausanne" , "Schweiz" , 129273, false ), City( "Paris" , "France" , 2203817, true ), City( "New York" , "USA" , 8363710 , false ), City( "Berlin" , "Germany" , 3416300, true ), City( "Tokyo" , "Japan" , 12787981, true )) val view = new ListView(items) val cityNames = view.selection.items.map(_.name)
  • 44. Renderers From the last example... val view = new ListView(items) { renderer = ListView.Renderer(_.name) } From the last example...
  • 45. Custom Painting val panel = new Panel { background = Color.blue override protected def paintComponent(g: Graphics2D) { super .paintComponent(g) g setColor Color.red for (x <- 0 until size.width by 10) g.drawLine(x, 0, x, size.height) } }
  • 46. Fun with mixins trait BlueBackground { this : Panel => background = Color.blue} trait BlackBackground { this : Panel => background = Color.black} trait VerticalLines extends Panel { override protected def paintComponent(g: Graphics2D) { super .paintComponent(g) g setColor Color.red for (x <- 0 until size.width by 10) g.drawLine(x, 0, x, size.height) } } trait HorizontalLines extends Panel { override protected def paintComponent(g: Graphics2D) { super .paintComponent(g) g setColor Color.white for (y <- 0 until size.height by 20) g.drawLine(0, y, size.width, y) } } trait Blobs extends Panel { override protected def paintComponent(g: Graphics2D) { super .paintComponent(g) g setColor Color.green for (x <- 0 until size.width by 20; y <- 0 until size.height by 40) g.fillOval(x-4, y-4, 8, 8) } }
  • 47. Fun with mixins val panel = new Panel with BlueBackground with HorizontalLines with VerticalLines with Blobs
  • 48. Fun with mixins val panel = new Panel with BlackBackground with Blobs with VerticalLines with HorizontalLines
  • 49. A basic complete app object SwingTalk extends SimpleSwingApplication { def top = new MainFrame { contents = new GridPanel(1, 4) { val nameLabel = new Label( &quot;Name&quot; ) val nameText = new TextField val helloButton = new Button( &quot;Say Hello&quot; ) val goodbyeButton = new Button( &quot;Say Goodbye&quot; ) listenTo(helloButton, goodbyeButton) contents ++= nameLabel :: nameText :: helloButton :: goodbyeButton :: Nil reactions += { case ButtonClicked(`helloButton`) => Dialog.showMessage(nameLabel, &quot;Hello, &quot; + nameText.text) case ButtonClicked(`goodbyeButton`) => Dialog.showMessage(nameLabel, &quot;Goodbye, &quot; + nameText.text) } } } }
  • 50. What's missing? Work in progress Drag N' Drop
  • 52. Better actor support for multi-threading EPFL are snowed under!
  • 53. Community contributions will make a big difference