How to customize column names in Scala table?

I am writing a Scala database management program and drew all the data in a two-dimensional ArrayBuffer array, where row 0 is the column names and the following lines contain information for each record in the table.When you try to put this in the table, ho = w, I'm going to assign column headers?

Syntax suggestions are welcome.

pseudo code:

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 in a table where Data (0) describes the column headers, and the subsequent rows describe the rows in the table, but I cannot figure out how to set the row headers.

+4
source share
2 answers

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

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

A bit complicated thing is that arrays are not covariant (see Why the example does not compile, how does it work (co-, contra- and in)? ), Which means that Array[String] is not a subtype of Array[Any] . Therefore, you need to somehow turn one into the other: a map does the job.

In addition, to display the column names, 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 ) } } } 

Gives you a table:

table

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

+3
source

Assuming you're talking about swing, if you put your table in scrollpane and create your table model based on the displayed array buffer, the first row will be accepted as default column names.

0
source

Source: https://habr.com/ru/post/1380078/


All Articles