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

Scala Swing swapping out components

1 reply
Charles F. Munat
Joined: 2008-12-29,
User offline. Last seen 42 years 45 weeks ago.

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

Frank Teubler
Joined: 2009-01-22,
User offline. Last seen 3 years 37 weeks ago.
Re: Scala Swing swapping out components

Charles 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
>

/*
* RemoveDemo.scala
*
* the removeButton removes the disappearButton
*/

package removedemo

import scala.swing.{SimpleGUIApplication, MainFrame, Button, BorderPanel}
import scala.swing.event.ButtonClicked

object RemoveDemo extends SimpleGUIApplication {

def top = new MainFrame {
title = "RemoveDemo"

contents = new BorderPanel {
import BorderPanel._

val removeButton = new Button("remove")
val disappearButton = new Button("disappear")

add(removeButton, Position.North)
add(disappearButton,Position.Center)

listenTo(removeButton)

reactions += {
case ButtonClicked(`removeButton`) => {
disappearButton.visible=false
_contents-=disappearButton
}
}
}
}
}

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