4

I am writing a Scala program to manage a database, and have drawn all of the data into a 2-dimensional ArrayBuffer where row 0 is the column names, and the subsequent rows contain the info for each entry in the table. When trying to put this into a Table, ho=w do I go about assigning the Column headers?

Syntax suggestions would be greatly appreciated.

Pseudocode:

Data=ArrayBuffer()
Data(0)={"Name","Birthday","ID"}
Data(1)={"Bob", "07/19/1986", "2354"}
Data(2)={"Sue", "05/07/1980", "2355"}
Data(3)={"Joe", "08/12/1992", "2356"}
Data(4)={"Jim", "11/20/1983", "2357"}

I want to put this into a Table where Data(0) describes the column headers, and the subsequent rows describe rows in the table, but I can't figure out how to set the row headers.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Museless
  • 137
  • 2
  • 10
  • You probably need to add additional information to this question. I think even pseudo code on what you're trying to do may help. – huynhjl Nov 08 '11 at 01:34

2 Answers2

3

The easiest way to put data in a Table is to use its constructor:

new Table (rowData: Array[Array[Any]], columnNames: Seq[_]) 

The slightly tricky thing here is that arrays are not covariant (see Why doesn't the example compile, aka how does (co-, contra-, and in-) variance work?), which means that an Array[String] is not a subtype of Array[Any]. So you need some way of turning one into the other: a map does the job.

Also, for the column names to show, you need to put the table in a ScrollPane.

import swing._
import collection.mutable.ArrayBuffer

object Demo extends SimpleSwingApplication {

  val data = ArrayBuffer(
    Array("Name","Birthday","ID"),
    Array("Bob", "07/19/1986", "2354"),
    Array("Sue", "05/07/1980", "2355")
  )

  def top = new MainFrame {
    contents = new ScrollPane {
      contents = new Table(
        data.tail.toArray map (_.toArray[Any]),
        data.head
      )
    }
  }
}

Will give you a table:

table

Edit: you can also use a cast: data.tail.toArray.asInstanceOf[Array[Array[Any]]], which is more efficient than mapping.

Community
  • 1
  • 1
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
  • So, that sounds like exactly what I want, but I am using and array of ArrayBuffers instead of an array of Arrays... I am not sure if that is the problem, but when I type in your code to display my buffer, I get an ArrayIndexOutOfBounds error, that I can't understand, and I am not sure how is being drawn from that code. Is this that I am using ArrayBuffers, or am I missing something else? – Museless Nov 09 '11 at 02:33
  • @Museless The code above will work just as well with ArrayBuffers, although the point of ArrayBuffers is that they're expandable, so there's no point using them for a fixed number of columns. There is no way the code above can give you an out of bounds error. The exception should tell you which line it's occurring on. – Luigi Plinge Nov 09 '11 at 04:03
0

assuming you are talking of swing, if you put your table inside a scrollpane and create your table model based on the array buffer shown, the first row will be taken as column names by default.

aishwarya
  • 1,970
  • 1
  • 14
  • 22
  • Not sure how to go about settung up a table model. My few feeble attempts at doing so have led to alot of Code-Not-Running syndrome. What is the syntax for this in 2.8? \n right now, I am simply updating the cells in a for-loop, because I haven't been able to find an easier way to update the data in the table, but this still leaves me with "A", "B", "C", "D", etc for my column headers – Museless Nov 08 '11 at 04:28
  • you can refer to [this code](https://github.com/asinghal/SlateIDE/blob/master/src/net/slate/gui/ResultsTabbedPane.scala) for some examples. – aishwarya Nov 08 '11 at 05:01