- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Scala Swing swapping out components
Thu, 2009-03-26, 02:00
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 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
}
}
}
}
}