Table headers disappear without ScrollPane

329 views Asked by At

I wrote the following simple code to test scala.swing.Table:

// java version "1.7.0_01"
// Scala code runner version 2.9.1.final
// Windows 7 Ultimate 64-bit
import scala.swing._

object TableHeaderVisible extends SimpleSwingApplication {
    override def top = new MainFrame {
        preferredSize = new Dimension(300, 200)
//      contents = new ScrollPane(table)
        contents = table
    }
    lazy val table = new Table(model, Seq("fruit", "animal")) // with Scrollable
    lazy val model = Array(
            Array("orange", "dog"),
            Array("apple",  "cat")).asInstanceOf[Array[Array[Any]]]
}

This produces no table headers, "fruit" and "animal".

Why?

I can do that with

contents = new ScrollPane(table)

not

contents = table.

But whether using ScrollPane or not should not affect whether table headers are visible or not, I think.

Is there any incorrect code ... in my code? or in scala.swing._ library?

Or is there any reason to justify invisible header[s] without ScrollPane?

2

There are 2 answers

1
Dan Burton On

Swing was originally written for Java, which definitely does not have lazy vals. My bet is that if you avoid lazy vals, the problem will disappear. If Scala actually intended to support lazy vals for the swing library, then this might be a bug.

0
kleopatra On

Don't know anything about scala.

In the underlying Swing, the reason is that there are two distinct components - JTable and an optional JTableHeader. While the header is configured to use the same TableColumnModel as the table itself (plus some wiring) always, its actual addition to the component hierarchy is managed by the table only if the table is contained in a scrollPane. If it is not in a scrollPane, the client code is responsible for inserting it where it makes most sense, f.i. in a panel above the table. In fact, there is nothing the table itself can reasonably do in that case, the surroundings can differ wildly