views:

33

answers:

2

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()
A: 

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
Thanks! Also, based on your reference, I posted an example without using the scrollPane
Matthew
A: 

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()
Matthew