Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala.swing Box equivalent

Tags:

scala

swing

There is scala.swing.BoxPanel, but it seems to miss the point, as there are no equivalents to javax.swing.Box factory methods createHorizontalStrut, createHorizontalGlue, createVerticalStrut, and createVerticalGlue. Also those methods return instances of java.awt.Component and thus cannot be submitted to scala.swing.Component.wrap.

Is there any simple workaround to creating spacing and glue with scala.swing.BoxPanel? If not, is there any existing open source library wrapping the functionality of javax.swing.Box?

like image 667
0__ Avatar asked Feb 18 '26 18:02

0__


2 Answers

I have always used the following for glues and struts (you can run it in REPL to test):

import swing._
import Swing._ // object with many handy functions and implicits

val panel = new BoxPanel(Orientation.Vertical) {
  contents += new Label("header")
  contents += VStrut(10)
  contents += new Label("aoeu")
  contents += VGlue
  contents += new Label("footer")
}

new Frame { contents = panel; visible = true }

There are methods for HGlue and HStrut, as well.

like image 66
Rogach Avatar answered Feb 21 '26 10:02

Rogach


There's all sorts of functionality missing in the Swing library.

Here's my solution for glues and struts:

import javax.{swing => jsw}
class HorzPanel extends BoxPanel(Orientation.Horizontal) {
  def glue = { peer.add(jsw.Box.createHorizontalGlue); this }
  def strut(n: Int) = { peer.add(jsw.Box.createHorizontalStrut(n)); this }
}
object HorzPanel {
  def apply(cs: Component*) = new HorzPanel { contents ++= cs }
}
class VertPanel extends BoxPanel(Orientation.Vertical) {
  def glue = { peer.add(jsw.Box.createVerticalGlue); this }
  def strut(n: Int) = { peer.add(jsw.Box.createVerticalStrut(n)); this }
}
object VertPanel {
  def apply(cs: Component*) = new VertPanel { contents ++= cs }
}

When you want to add glue or a strut, you just state "glue" or "strut(n)" inline:

new VertPanel {
  contents += new Label("Hi")
  glue
  contents += new Label("there")
}

(assuming you're using the contents += method; it doesn't actually give you an object to add, so you can't assemble it with your other items and add them as a collection.)

like image 44
Rex Kerr Avatar answered Feb 21 '26 09:02

Rex Kerr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!