The groovy swing builder is one of Groovys best features. It becomes quite easy to build a gui with the builder. In some cases you may like to add a custom Java swing component to your swing builder. For such cases the swing builder is equipped with the widget() method. The following code uses the swing builder tabbed pane and add a custom component implemented in the getSearchPanel() method as tab.
package de.acidum.candystreams.gui; import groovy.swing.SwingBuilder import javax.swing.SwingUtilities import javax.swing.JFrame import java.awt.BorderLayout import javax.swing.JLabel import javax.swing.JLabel import javax.swing.JPanel public class CandystreamsClient implements Runnable { CandystreamsClient(){ // set system property to use mac os menu bar System.setProperty("apple.laf.useScreenMenuBar", "true") // set the application name for mac os System.setProperty("com.apple.mrj.application.apple.menu.about.name", "CandyStreams") } public void run() { def swing = new SwingBuilder() def frame = swing.frame(title: "CandyStreams", defaultCloseOperation: JFrame.DISPOSE_ON_CLOSE, size: [800, 600]){ // add a simple menu bar menuBar() { menu(text: "File", mnemonic: 'F') { menuItem(text: "Exit", mnemonic: 'X', actionPerformed: { System.exit(0) }) } } // add a tabbed pane tabbedPane() { panel(title:'Panel 1', tabBackground:java.awt.Color.GREEN,tabToolTip:'Panel1') // add custom swing component widget(title:'Search', getSearchPanel()) } } frame.show() } public getSearchPanel(){ JPanel panel = new JPanel() panel.add(new JLabel("Search Panel")) return panel } public static void main(String[] args) { SwingUtilities.invokeLater(new CandystreamsClient()) } }
Leave a Reply