Search code examples
groovyswingbuilder

How to add a header to a Groovy SwingBuilder table?


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

Solution

  • 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