#1606 How to Set Row Data in Table

RedDevil Mon 8 Aug 2011

Hi there, I am still exploring Fantom and like very much what I see. I found the Table example released 18 Jul 11 but I can not figure out how to set the row data for example from a file.

andy Mon 8 Aug 2011

If you're updating your TableModel after its first displayed, you will need to call table.rebuildAll to force the table to refresh its content.

brian Mon 8 Aug 2011

Here is a super, simple but complete example of a program which reads data from a CSV file and displays it a table:

using gfx
using fwt
using util

class TableLoad : AbstractMain
{
  @Arg File? file
  override Int run()
  {
    Window 
    { 
      content = Table { model = CsvModel(file) }
      size = Size(800,600)
    }.open
    return 0
  }
}

internal class CsvModel : TableModel
{
  new make(File f) 
  { 
    rows = CsvInStream(f.in).readAllRows
    cols = rows[0]  // assume first row if header
    rows = rows[1..-1] 
  }
  override Int numRows() { rows.size }
  override Int numCols() { cols.size }
  override Str header(Int c) { cols[c] }
  override Str text(Int c, Int r) { rows[r][c] }
  Str[] cols
  Str[][] rows
}

RedDevil Tue 9 Aug 2011

Thanks Guys - making much progress. I combined the info above with the Table demo and I am now stumped with col alignment. In the following code the first column does not center align. What am I missing here? Thanks.

using gfx
using fwt
**
** TableDemo illustrates different features of the Table widget
**
class TableDemo
{
  Void main()
{
    // model and table
    model := Model()
    table := Table { it.model = model }
    Window
    {
      title = "Table Demo"
      EdgePane
      {
        center = table
      },;
      size = Size(400,600)
    }.open
  }
}

class Model : TableModel
{
  Halign halignVal := Halign.center
  override Str header(Int c) { cols[c] }
  override Int numRows := 3
  override Int numCols := 3
  override Halign halign(Int c) { halignVal }
  override Str text(Int c, Int r) { rows[r][c] }
  override Int? prefWidth(Int c) { 70 }
  Str[] cols := ["LLVM", "CV","Here"]
  Str[][] rows := [["1","2","3"],["4","5","6"],["7","8","9"]]
}

brian Tue 9 Aug 2011

I see that behavior too, the aligmnent doesn't take for the first column. I think this is a SWT issue though - see Bug 16223 (sounds like its a limitation of Windows)

qualidafial Tue 9 Aug 2011

I've come across this in my own SWT development. This is a Windows limitation.

RedDevil Wed 10 Aug 2011

Again many thanks for all the help. I could not get the setColVisible to work but by setting col0 to 0 width it achieves what I need. Regards.

using gfx
using fwt
**
** TableDemo illustrates different features of the Table widget
**
class TableDemo
{
  Void main()
{
    // model and table
    model := Model()
    table := Table { it.model = model }
    Window
    {
      title = "Table Demo"
      EdgePane
      {
        center = table
      },;
      size = Size(400,600)
//      table.setColVisible(0,false)
      table.refreshAll
    }.open
  }
}

class Model : TableModel
{
  Halign halignVal := Halign.center
  override Str header(Int c) { cols[c] }
  override Int numRows := 3
  override Int numCols := 4
  override Halign halign(Int c) { halignVal }
  override Str text(Int c, Int r) { rows[r][c] }
  override Int? prefWidth(Int c) { wd[c] }
  Int[] wd := [0,50,70,90]
  Str[] cols := ["","Nr", "CVLL","CPD"]
  Str[][] rows := [["","1","2","3"],["","4","5","6"],["","7","8","9"]]
}

Login or Signup to reply.