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

Fwd: Scala Swing swapping out components

4 replies
Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Setting the content a second time doesn't work? content = ... or whatever you wrote to set the initial component doesn't work afterward? Regular (Java) Swing has CardLayout (since 1.6 there's another similar layout something like Stack, I forget what it's called). In actual Swing, layout managers are a property of a descendant of Container (JComponent). But ScalaSwing, in order to provide layouts with type safety, and given the rarity of changing layouts on a component, defines components with built in layouts. So if there is not yet a CardPanel etc. in ScalaSwing, you have to write it yourself, similarly to the provided components, which should not be too hard. Maybe your implementation can then be added to the next release!

On Wed, Mar 25, 2009 at 9:00 PM, Charles F. Munat <chas@munat.com> wrote:
I am building a desktop GUI app in Scala using Swing.

I have a menu bar at the top of the window (File, Edit, etc.) in the North position of a BorderLayout (which is itself in a MainFrame). When the user clicks on an item in the menu, I want to swap out the Component in the Center position of the Border Layout (at runtime). I can't figure out how to do this (or something equivalent).

The add method on BorderLayout is protected.

Are there any examples? I looked at the Scala Swing Test suite. It uses a tabbed panel. But I don't want tabs, I want to control things via the Menubar. (Maybe I can hide the tabs?)

Any help appreciated. I have to demo this tomorrow.

Thanks,

Chas. Munat
Seattle



Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Fwd: Scala Swing swapping out components
Could you post your source file?



On Wed, Mar 25, 2009 at 10:50 PM, Charles F. Munat <chas@munat.com> wrote:
When I try to set the content:

top.contents = dprPanel

I get this:

recursive value dprPanel needs type

But I have no clue what this means. contents is set to topPanel at compile time. Both dprPanel and topPanel are defined as BorderLayouts inside of top (which is a MainFrame). If drpPanel is recursive, why isn't topPanel? They're virtually identical. In fact, if I try to set content to topPanel (which it is already set as), I get the same error message.

What does this mean? Anyone know?

Chas.

Naftoli Gugenheim wrote:
Setting the content a second time doesn't work? content = ... or whatever you wrote to set the initial component doesn't work afterward?
Regular (Java) Swing has CardLayout (since 1.6 there's another similar layout something like Stack, I forget what it's called).
In actual Swing, layout managers are a property of a descendant of Container (JComponent). But ScalaSwing, in order to provide layouts with type safety, and given the rarity of changing layouts on a component, defines components with built in layouts. So if there is not yet a CardPanel etc. in ScalaSwing, you have to write it yourself, similarly to the provided components, which should not be too hard. Maybe your implementation can then be added to the next release!

On Wed, Mar 25, 2009 at 9:00 PM, Charles F. Munat <chas@munat.com <mailto:chas@munat.com>> wrote:

   I am building a desktop GUI app in Scala using Swing.

   I have a menu bar at the top of the window (File, Edit, etc.) in the
   North position of a BorderLayout (which is itself in a MainFrame).
   When the user clicks on an item in the menu, I want to swap out the
   Component in the Center position of the Border Layout (at runtime).
   I can't figure out how to do this (or something equivalent).

   The add method on BorderLayout is protected.

   Are there any examples? I looked at the Scala Swing Test suite. It
   uses a tabbed panel. But I don't want tabs, I want to control things
   via the Menubar. (Maybe I can hide the tabs?)

   Any help appreciated. I have to demo this tomorrow.

   Thanks,

   Chas. Munat
   Seattle




Naftoli Gugenheim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Fwd: Scala Swing swapping out components

In any case, I don't think recursive means in terms of container
heirarchy but in terms of the compiler being able to determine its
type. Try adding a type annotation (v : T) and see what happens...

On 3/25/09, Naftoli Gugenheim wrote:
> Could you post your source file?
>
>
>
> On Wed, Mar 25, 2009 at 10:50 PM, Charles F. Munat wrote:
>
>> When I try to set the content:
>>
>> top.contents = dprPanel
>>
>> I get this:
>>
>> recursive value dprPanel needs type
>>
>> But I have no clue what this means. contents is set to topPanel at
>> compile
>> time. Both dprPanel and topPanel are defined as BorderLayouts inside of
>> top
>> (which is a MainFrame). If drpPanel is recursive, why isn't topPanel?
>> They're virtually identical. In fact, if I try to set content to topPanel
>> (which it is already set as), I get the same error message.
>>
>> What does this mean? Anyone know?
>>
>> Chas.
>>
>> Naftoli Gugenheim wrote:
>>
>>> Setting the content a second time doesn't work? content = ... or
>>> whatever
>>> you wrote to set the initial component doesn't work afterward?
>>> Regular (Java) Swing has CardLayout (since 1.6 there's another similar
>>> layout something like Stack, I forget what it's called).
>>> In actual Swing, layout managers are a property of a descendant of
>>> Container (JComponent). But ScalaSwing, in order to provide layouts with
>>> type safety, and given the rarity of changing layouts on a component,
>>> defines components with built in layouts. So if there is not yet a
>>> CardPanel
>>> etc. in ScalaSwing, you have to write it yourself, similarly to the
>>> provided
>>> components, which should not be too hard. Maybe your implementation can
>>> then
>>> be added to the next release!
>>>
>>> On Wed, Mar 25, 2009 at 9:00 PM, Charles F. Munat
>>> >> chas@munat.com>> wrote:
>>>
>>> I am building a desktop GUI app in Scala using Swing.
>>>
>>> I have a menu bar at the top of the window (File, Edit, etc.) in the
>>> North position of a BorderLayout (which is itself in a MainFrame).
>>> When the user clicks on an item in the menu, I want to swap out the
>>> Component in the Center position of the Border Layout (at runtime).
>>> I can't figure out how to do this (or something equivalent).
>>>
>>> The add method on BorderLayout is protected.
>>>
>>> Are there any examples? I looked at the Scala Swing Test suite. It
>>> uses a tabbed panel. But I don't want tabs, I want to control things
>>> via the Menubar. (Maybe I can hide the tabs?)
>>>
>>> Any help appreciated. I have to demo this tomorrow.
>>>
>>> Thanks,
>>>
>>> Chas. Munat
>>> Seattle
>>>
>>>
>>>
>>>
>

Charles F. Munat
Joined: 2008-12-29,
User offline. Last seen 42 years 45 weeks ago.
Re: Fwd: Scala Swing swapping out components

It's very rough as I'm playing around with things, but here is the gist
of it:

package xxx

import scala.swing._
import scala.swing.event._

import com.sleepycat.dbxml.{XmlContainer,XmlException,XmlManager}

import org.jfree.data.general.{DefaultPieDataset}
import org.jfree.chart.{JFreeChart,ChartFactory,ChartPanel,ChartUtilities}
import org.jfree.data.xy.{XYDataset}
import
org.jfree.data.time.{TimeSeries,Day,Month,Year,TimeSeriesCollection,TimePeriodAnchor}
import org.jfree.chart.plot.{XYPlot}
import org.jfree.chart.renderer.xy.{XYItemRenderer,XYLineAndShapeRenderer}
import org.jfree.chart.axis.{PeriodAxis}
import org.jfree.chart.axis.{PeriodAxisLabelInfo}
import org.jfree.ui.{RectangleInsets}

import java.text.{SimpleDateFormat}
import java.util.{TimeZone,Date}
import java.awt.{Font,Color,BasicStroke}

import com.toedter.calendar.{JDateChooser}

object Main extends SimpleGUIApplication {
def top = new MainFrame {
import java.util.Calendar._
var d = java.util.Calendar.getInstance()
val students = new
ComboBox(Student.getStudentList.map(_.toString).toList)
val reportDates = Component.wrap(new JDateChooser())
val selectButton = new Button("Select")

val menu = new MenuBar {
import scala.swing.Dialog._
val chartProgress = new MenuItem("Chart Progress")

contents += new Menu("Report") {
contents += chartProgress
contents += new MenuItem(Action("Summarize Progress") {
top.contents = dprPanel
})
}
contents += new Menu("Help") {
contents += new MenuItem("Contents")
}

}

val studentsPanel = new BorderPanel {
import BorderPanel.Position._
import java.util.Calendar._

val form = StudentForm.ui(s2)
val formPanel = new BorderPanel {
add(formLabel, North)
add(form, Center)
}
val studentForm = new ScrollPane(formPanel)
studentForm.yLayoutAlignment = 0
val studentTable = new StudentTable(600,400)

add(new SplitPane(Orientation.Vertical, studentForm,
studentTable) {
oneTouchExpandable = true
continuousLayout = true
}, Center)
}

val dprPanel = new BorderPanel {
import BorderPanel.Position._

val svnf = new BorderPanel {
val formLabel = new Label("Set descriptors")
formLabel.font = new Font("SansSerif", Font.BOLD, 16)

border = Swing.EmptyBorder(5, 10, 10, 10)

val valueNameForm = ScaleValueNameForm.ui

add(formLabel, North)
add(valueNameForm, Center)
}

val ef = new BorderPanel {
val formLabel = new Label("Set expectations")
formLabel.font = new Font("SansSerif", Font.BOLD, 16)

val valueNameForm = ReportExpectationForm.ui

add(formLabel, North)
add(valueNameForm, Center)
}

val pf = new BorderPanel {
val formLabel = new Label("Set periods")
formLabel.font = new Font("SansSerif", Font.BOLD, 16)

val valueNameForm = ReportPeriodForm.ui

add(formLabel, North)
add(valueNameForm, Center)
}

val bottomBar = new GridBagPanel {
import GridBagPanel._

val c = new Constraints
val shouldFill = true

if (shouldFill) {
c.fill = Fill.Horizontal
c.weightx = 0.5
c.ipady = 20
}

val saveButton = new Button("Save")
c.fill = Fill.None
c.anchor = Anchor.East
c.gridx = 1
c.gridy = 0
c.ipady = 0
layout(saveButton) = c
}

add(svnf, West)
add(ef, Center)
add(pf, East)
add(bottomBar, South)
}

contents = studentsPanel
menuBar = menu
}

}

Naftoli Gugenheim wrote:
> Could you post your source file?
>
>
>
> On Wed, Mar 25, 2009 at 10:50 PM, Charles F. Munat > wrote:
>
> When I try to set the content:
>
> top.contents = dprPanel
>
> I get this:
>
> recursive value dprPanel needs type
>
> But I have no clue what this means. contents is set to topPanel at
> compile time. Both dprPanel and topPanel are defined as
> BorderLayouts inside of top (which is a MainFrame). If drpPanel is
> recursive, why isn't topPanel? They're virtually identical. In fact,
> if I try to set content to topPanel (which it is already set as), I
> get the same error message.
>
> What does this mean? Anyone know?
>
> Chas.
>
> Naftoli Gugenheim wrote:
>
> Setting the content a second time doesn't work? content = ... or
> whatever you wrote to set the initial component doesn't work
> afterward?
> Regular (Java) Swing has CardLayout (since 1.6 there's another
> similar layout something like Stack, I forget what it's called).
> In actual Swing, layout managers are a property of a descendant
> of Container (JComponent). But ScalaSwing, in order to provide
> layouts with type safety, and given the rarity of changing
> layouts on a component, defines components with built in
> layouts. So if there is not yet a CardPanel etc. in ScalaSwing,
> you have to write it yourself, similarly to the provided
> components, which should not be too hard. Maybe your
> implementation can then be added to the next release!
>
> On Wed, Mar 25, 2009 at 9:00 PM, Charles F. Munat
> >> wrote:
>
> I am building a desktop GUI app in Scala using Swing.
>
> I have a menu bar at the top of the window (File, Edit, etc.)
> in the
> North position of a BorderLayout (which is itself in a
> MainFrame).
> When the user clicks on an item in the menu, I want to swap
> out the
> Component in the Center position of the Border Layout (at
> runtime).
> I can't figure out how to do this (or something equivalent).
>
> The add method on BorderLayout is protected.
>
> Are there any examples? I looked at the Scala Swing Test
> suite. It
> uses a tabbed panel. But I don't want tabs, I want to control
> things
> via the Menubar. (Maybe I can hide the tabs?)
>
> Any help appreciated. I have to demo this tomorrow.
>
> Thanks,
>
> Chas. Munat
> Seattle
>
>
>
>

Detering Dirk
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
RE: Fwd: Scala Swing swapping out components

Hi Charles,

not directly connected to your error, I think, but:

I had a quick look on the source, simply as I'm interested
in seeing some Scala Swing examples (Had no time to dig
into it myself up to now).

What irritated me is:

> object Main extends SimpleGUIApplication {
> def top = new MainFrame {

Here top is defined as a parameterless function creating a new
MainFrame.

> contents += new Menu("Report") {
> contents += chartProgress
> contents += new MenuItem(Action("Summarize Progress") {
> top.contents = dprPanel
> })
> }

here you call 'top.contents =' and by calling 'top' I suppose
you are creating a new MainFrame.
Is that right?

KR
Det

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