This page is no longer maintained — Please continue to the home page at www.scala-lang.org

(Probably) Simple Question: Scala + Swing

3 replies
Graham Lea
Joined: 2009-11-09,
User offline. Last seen 42 years 45 weeks ago.

Hi,

I'm having trouble getting a little Scala Swing app going.
The code is below.
The problem is that my pack() method has no effect - the app still
shows in the top-right corner with the default size.
I've done lots of Swing development before, but this is the first time
(a) with Scala and (b) on a Mac, so I'm not sure which to blame.
The executable is 'TestApp', at the bottom.
Cheers,

Graham

import java.awt.{Dimension, BorderLayout, Color, Font}
import scala.swing._

trait Generator {
}

trait Uploader {
}

abstract class GeneratorAndUploaderUI(val title: String) extends
SimpleGUIApplication {
ui =>
val generator: Generator
val uploader: Uploader

def top = new MainFrame {
title = ui.title

val titleLabel = new Label {
text = ui.title
font = font.deriveFont(font.getSize2D * 2)
horizontalTextPosition = Alignment.Center
border = Swing.EmptyBorder(10)
}

val uploadDraftButton = new Button {
text = "Upload Draft"
}

val reviewDraftButton = new Button {
text = "Review Draft"
}

val uploadFinalButton = new Button {
text = "Upload Final"
}

val viewFinalButton = new Button {
text = "View Final"
}

contents = new BorderPanel() {
import BorderPanel.Position._
add(titleLabel, North)
add(new GridPanel(0, 1) {
List(uploadDraftButton, reviewDraftButton, uploadFinalButton,
viewFinalButton) foreach {contents += _}
border = Swing.EmptyBorder(20, 10, 20, 10)
vGap = 20
}, Center)
}

override def pack() = {
super.pack
size = (1400, 1600)
println("Size set")
peer.setLocationRelativeTo(null)
println("location set")
this
}
}
}

object TestApp extends GeneratorAndUploaderUI("Test Application") {
val generator = new Generator{}
val uploader = new Uploader{}
}

Ken Scambler
Joined: 2009-11-07,
User offline. Last seen 42 years 45 weeks ago.
Re: (Probably) Simple Question: Scala + Swing

Hi Graham,
Overriding pack() is generally not the "done thing" in a Swing app.

If you want the frame to have a large size, get rid of your pack()
implementation, and simply have "size = (1400,600)" inside the body of your
MainFrame - I ran this on Windows and it worked. The intention of the pack
method is to resize the frame to the minimum size where components are still
displayed normally, and this shouldn't be changed to arbitrary semantics.

In any case, there is little benefit to be gained from extending
SimpleGUIApplication, (which calls top.pack()), so you may find it easier to
jettison it and just start from a regular module with a main method.

All the best,
Ken

Graham Lea-4 wrote:
>
> Hi,
>
> I'm having trouble getting a little Scala Swing app going.
> The code is below.
> The problem is that my pack() method has no effect - the app still
> shows in the top-right corner with the default size.
> I've done lots of Swing development before, but this is the first time
> (a) with Scala and (b) on a Mac, so I'm not sure which to blame.
> The executable is 'TestApp', at the bottom.
> Cheers,
>
> Graham
>
>
>
> import java.awt.{Dimension, BorderLayout, Color, Font}
> import scala.swing._
>
> trait Generator {
> }
>
> trait Uploader {
> }
>
> abstract class GeneratorAndUploaderUI(val title: String) extends
> SimpleGUIApplication {
> ui =>
> val generator: Generator
> val uploader: Uploader
>
> def top = new MainFrame {
> title = ui.title
>
> val titleLabel = new Label {
> text = ui.title
> font = font.deriveFont(font.getSize2D * 2)
> horizontalTextPosition = Alignment.Center
> border = Swing.EmptyBorder(10)
> }
>
> val uploadDraftButton = new Button {
> text = "Upload Draft"
> }
>
> val reviewDraftButton = new Button {
> text = "Review Draft"
> }
>
> val uploadFinalButton = new Button {
> text = "Upload Final"
> }
>
> val viewFinalButton = new Button {
> text = "View Final"
> }
>
> contents = new BorderPanel() {
> import BorderPanel.Position._
> add(titleLabel, North)
> add(new GridPanel(0, 1) {
> List(uploadDraftButton, reviewDraftButton, uploadFinalButton,
> viewFinalButton) foreach {contents += _}
> border = Swing.EmptyBorder(20, 10, 20, 10)
> vGap = 20
> }, Center)
> }
>
> override def pack() = {
> super.pack
> size = (1400, 1600)
> println("Size set")
> peer.setLocationRelativeTo(null)
> println("location set")
> this
> }
> }
> }
>
> object TestApp extends GeneratorAndUploaderUI("Test Application") {
> val generator = new Generator{}
> val uploader = new Uploader{}
> }
>
>

Graham Lea
Joined: 2009-11-09,
User offline. Last seen 42 years 45 weeks ago.
Re: (Probably) Simple Question: Scala + Swing

Hi Ken,

Thanks for the reply.
I did feel dirty overriding pack(), and I would never have done it in
Java, but looking at SimpleGUIApplication, it looked like the way
scala.swing wanted me to do it.
Regardless, your reply inspired me to dispose of my pack hack and
investigate other means, by which I eventually came to this:

reactions += { case WindowActivated(_) =>
peer.setLocationRelativeTo(null) }

which works perfectly.

Thanks again for your pointer away from the wrong direction. ; )

Cheers,

Graham.

On 10/11/2009, at 6:17 PM, Ken Scambler wrote:

>
> Hi Graham,
> Overriding pack() is generally not the "done thing" in a Swing app.
>
> If you want the frame to have a large size, get rid of your pack()
> implementation, and simply have "size = (1400,600)" inside the body
> of your
> MainFrame - I ran this on Windows and it worked. The intention of
> the pack
> method is to resize the frame to the minimum size where components
> are still
> displayed normally, and this shouldn't be changed to arbitrary
> semantics.
>
> In any case, there is little benefit to be gained from extending
> SimpleGUIApplication, (which calls top.pack()), so you may find it
> easier to
> jettison it and just start from a regular module with a main method.
>
> All the best,
> Ken
>
>
> Graham Lea-4 wrote:
>>
>> Hi,
>>
>> I'm having trouble getting a little Scala Swing app going.
>> The code is below.
>> The problem is that my pack() method has no effect - the app still
>> shows in the top-right corner with the default size.
>> I've done lots of Swing development before, but this is the first
>> time
>> (a) with Scala and (b) on a Mac, so I'm not sure which to blame.
>> The executable is 'TestApp', at the bottom.
>> Cheers,
>>
>> Graham
>>
>>
>>
>> import java.awt.{Dimension, BorderLayout, Color, Font}
>> import scala.swing._
>>
>> trait Generator {
>> }
>>
>> trait Uploader {
>> }
>>
>> abstract class GeneratorAndUploaderUI(val title: String) extends
>> SimpleGUIApplication {
>> ui =>
>> val generator: Generator
>> val uploader: Uploader
>>
>> def top = new MainFrame {
>> title = ui.title
>>
>> val titleLabel = new Label {
>> text = ui.title
>> font = font.deriveFont(font.getSize2D * 2)
>> horizontalTextPosition = Alignment.Center
>> border = Swing.EmptyBorder(10)
>> }
>>
>> val uploadDraftButton = new Button {
>> text = "Upload Draft"
>> }
>>
>> val reviewDraftButton = new Button {
>> text = "Review Draft"
>> }
>>
>> val uploadFinalButton = new Button {
>> text = "Upload Final"
>> }
>>
>> val viewFinalButton = new Button {
>> text = "View Final"
>> }
>>
>> contents = new BorderPanel() {
>> import BorderPanel.Position._
>> add(titleLabel, North)
>> add(new GridPanel(0, 1) {
>> List(uploadDraftButton, reviewDraftButton, uploadFinalButton,
>> viewFinalButton) foreach {contents += _}
>> border = Swing.EmptyBorder(20, 10, 20, 10)
>> vGap = 20
>> }, Center)
>> }
>>
>> override def pack() = {
>> super.pack
>> size = (1400, 1600)
>> println("Size set")
>> peer.setLocationRelativeTo(null)
>> println("location set")
>> this
>> }
>> }
>> }
>>
>> object TestApp extends GeneratorAndUploaderUI("Test Application") {
>> val generator = new Generator{}
>> val uploader = new Uploader{}
>> }
>>
>>
>

gerferra
Joined: 2009-08-26,
User offline. Last seen 1 year 38 weeks ago.
Re: (Probably) Simple Question: Scala + Swing
Hi Graham,

This is what I'm doing to center mi windows on the screen:
def top = new MainFrame {   title = "Test Window"    ....     centerOnScreen}
centerOnScreen is defined as peer.setLocationRelativeTo(null)
I don't remember if it has to be the last statement of the MainFrame definition (don't have the time to test it now).

Regards,Germán.
On Wed, Nov 11, 2009 at 9:13 AM, Graham Lea <graham [at] grahamlea [dot] com> wrote:
Hi Ken,

Thanks for the reply.
I did feel dirty overriding pack(), and I would never have done it in Java, but looking at SimpleGUIApplication, it looked like the way scala.swing wanted me to do it.
Regardless, your reply inspired me to dispose of my pack hack and investigate other means, by which I eventually came to this:

   reactions += { case WindowActivated(_) => peer.setLocationRelativeTo(null) }

which works perfectly.

Thanks again for your pointer away from the wrong direction.   ; )

Cheers,

Graham.

On 10/11/2009, at 6:17 PM, Ken Scambler wrote:


Hi Graham,
Overriding pack() is generally not the "done thing" in a Swing app.

If you want the frame to have a large size, get rid of your pack()
implementation, and simply have "size = (1400,600)" inside the body of your
MainFrame - I ran this on Windows and it worked.  The intention of the pack
method is to resize the frame to the minimum size where components are still
displayed normally, and this shouldn't be changed to arbitrary semantics.

In any case, there is little benefit to be gained from extending
SimpleGUIApplication, (which calls top.pack()), so you may find it easier to
jettison it and just start from a regular module with a main method.

All the best,
Ken


Graham Lea-4 wrote:

Hi,

I'm having trouble getting a little Scala Swing app going.
The code is below.
The problem is that my pack() method has no effect - the app still
shows in the top-right corner with the default size.
I've done lots of Swing development before, but this is the first time
(a) with Scala and (b) on a Mac, so I'm not sure which to blame.
The executable is 'TestApp', at the bottom.
Cheers,

Graham



import java.awt.{Dimension, BorderLayout, Color, Font}
import scala.swing._

trait Generator {
}

trait Uploader {
}

abstract class GeneratorAndUploaderUI(val title: String) extends
SimpleGUIApplication {
 ui =>
 val generator: Generator
 val uploader: Uploader

 def top = new MainFrame {
   title = ui.title

   val titleLabel = new Label {
     text = ui.title
     font = font.deriveFont(font.getSize2D * 2)
     horizontalTextPosition = Alignment.Center
     border = Swing.EmptyBorder(10)
   }

   val uploadDraftButton = new Button {
     text = "Upload Draft"
   }

   val reviewDraftButton = new Button {
     text = "Review Draft"
   }

   val uploadFinalButton = new Button {
     text = "Upload Final"
   }

   val viewFinalButton = new Button {
     text = "View Final"
   }

   contents = new BorderPanel() {
     import BorderPanel.Position._
     add(titleLabel, North)
     add(new GridPanel(0, 1) {
       List(uploadDraftButton, reviewDraftButton, uploadFinalButton,
viewFinalButton) foreach {contents += _}
       border = Swing.EmptyBorder(20, 10, 20, 10)
       vGap = 20
     }, Center)
   }

   override def pack() = {
     super.pack
     size = (1400, 1600)
     println("Size set")
     peer.setLocationRelativeTo(null)
     println("location set")
     this
   }
 }
}

object TestApp extends GeneratorAndUploaderUI("Test Application") {
 val generator = new Generator{}
 val uploader = new Uploader{}
}



Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland