3

How do I add a header to the table defined below?

import groovy.swing.SwingBuilder

data = [[first:'qwer', last:'asdf'],
        [first:'zxcv', last:'tyui'],
        [first:'ghjk', last:'bnm']]

swing = new SwingBuilder()
frame = swing.frame(title:'table test'){
    table {
        tableModel( list : data ) {
            propertyColumn(header:'First Name', propertyName:'first')
            propertyColumn(header:'last Name', propertyName:'last')
        }
    }
}
frame.pack()
frame.show()
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Matt
  • 682
  • 5
  • 20

2 Answers2

5

If you put the table in a scrollPane, the headers appear:

import groovy.swing.SwingBuilder

data = [[first:'qwer', last:'asdf'],
        [first:'zxcv', last:'tyui'],
        [first:'ghjk', last:'bnm']]

swing = new SwingBuilder()
frame = swing.frame(title:'table test'){
  scrollPane {
    table {
        tableModel( list : data ) {
            propertyColumn(header:'First Name', propertyName:'first')
            propertyColumn(header:'last Name', propertyName:'last')
        }
    }
  }
}
frame.pack()
frame.show()

See item one on this page for an explanation why

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • Thanks! Also, based on your reference, I posted an example without using the scrollPane – Matt Aug 31 '10 at 17:25
1

Table header is a separate widget that must be added explicitly.

import groovy.swing.SwingBuilder
import java.awt.BorderLayout

data = [[first:'qwer', last:'asdf'],
        [first:'zxcv', last:'tyui'],
        [first:'ghjk', last:'bnm']]

swing = new SwingBuilder()
frame = swing.frame(title:'table test'){
    def tab = table(constraints:BorderLayout.CENTER) {
        tableModel( list : data ) {
            propertyColumn(header:'First Name', propertyName:'first')
            propertyColumn(header:'Last Name', propertyName:'last')
        }
    }
    widget(constraints:BorderLayout.NORTH, tab.tableHeader)
}
frame.pack()
frame.show()
Matt
  • 682
  • 5
  • 20