Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple SwingBuilder application that reads in a file of mapped color themes and visualizes the results
import groovy.swing.*
import java.awt.*
import javax.swing.*
* Reads in a map of color themes, each with five hex color values, and displays them in a Swing component.
assert args.size() == 1, 'The name or path to a file containing a themeMap script variable must be supplied on the command line'
def themeMapFileName = args[0]
Binding binding = new Binding()
new GroovyShell(binding).evaluate(new File(themeMapFileName))
assert binding.hasVariable('themeMap'), "${args[0]} file must contain a Map variable named themeMap"
def themeMap = binding.themeMap as TreeMap
def swing = new groovy.swing.SwingBuilder()
def mainPanel = swing.panel() {
boxLayout(axis: javax.swing.BoxLayout.Y_AXIS)
label(text: "Showing ${themeMap.size()} themes")
scrollPane() {
panel() {
boxLayout(axis: javax.swing.BoxLayout.Y_AXIS)
themeMap.each { key, value ->
panel(border: emptyBorder(3)) {
gridLayout(columns: 2, rows: 1)
label(text: key)
value.each {
def color = Color.decode("#" + it)
int colorSize = 50
label(opaque: true, toolTipText: it, background: color, foreground: color,
preferredSize: [colorSize, colorSize] as Dimension,
border: lineBorder(color:Color.WHITE, thickness:1))
def frame = swing.frame(title: 'Frame') {
scrollPane(constraints: SwingConstants.CENTER) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment