How to add a title to a Groovy SwingBuilder table?

How to add a title to the table 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()
+3
source share
2 answers

If you put the table in scrollPane, the headers will 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 point 1 on this page for an explanation of why

+5
source

The table title 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()
+1
source

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


All Articles