1

I am writing a Griffon/Groovy/Swing application. However, I do like using WYSIWYG GUI tools like Eclipe's WindowBuilder tool as well.

I am wondering if it is possible to combine the two approaches? I'd like to use SwingBuilder to manage view/model binding and some of the high level GUI tasks (like JDesktopPane and JInternalFrame), but have the contents designed into JFrames managed by WindowBuilder.

Here is a simple groovy script:

package example

import groovy.swing.SwingBuilder
import java.awt.BorderLayout as BL

count = 0
new SwingBuilder().edt {
  frame(title: 'Frame', size: [300, 300], show: true) {
    desktopPane() {
      internalFrame(visible: true, bounds: [25, 25, 200, 100]) {
        borderLayout()
        textlabel = label(text: 'Click the button!', constraints: BL.NORTH)
        button(text:'Click Me',
             actionPerformed: {count++; textlabel.text = "Clicked ${count} time(s)."; println "clicked"}, constraints:BL.SOUTH)
      }

      internalFrame(visible: true, bounds: [50, 50, 200, 100]).add(new ExamplePanel())
    }
  }
}

and this is the ExamplePanel subclass of JFrame:

package example;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JButton;

public class ExamplePanel extends JPanel {

    /**
     * Create the panel.
     */
    public ExamplePanel() {
        setLayout(new BorderLayout(0, 0));

        JLabel textLabel = new JLabel("Click the button!");
        add(textLabel, BorderLayout.NORTH);

        JButton button = new JButton("Click Me");
        add(button, BorderLayout.SOUTH);

    }

}

The groovy script creates a JDesktopPane and two visually identical JInternalFrames. The second JInternalFrame contains ExamplePanel, but it has no infrastructure for detecting button clicks or altering the content of the label.

Is there a groovy way to get the same behaviour in the ExamplePanel as I am getting in the SwingBuilder defined internalFrame?

spierepf
  • 2,774
  • 2
  • 30
  • 52

2 Answers2

1

Yes, you can embed any custom component using different alternatives:

  1. Pick an specific node. In your case ExamplePanel extends Panel thus you can use the panel() node in the following way

    internalFrame { panel(new ExamplePanel()) }

  2. Pick a generic node. You may use either widget or container. The former does not accept nesting while the latter does.

    internalFrame { container(new ExamplePanel()) { /* further customizations */ } }

  3. You may create your own node factory and register it with the builder. The Griffon guide has a section showing how it can be done. http://griffon-framework.org/guide/2.14.0/#_views_builder_customizers

Andres Almiray
  • 3,236
  • 18
  • 28
0

This is not an "answer" as it doesn't answer your question. However I want to point to another direction.

The GUI design tools often use some grid layout and the arbitrary positions created from your drawing. If you are using a layout properly, the GUI can respond to app resizing properly, while the grid layout from design tools often doesn't scale well with resizing.

There is a domain language for Swing layout called Miglayout. It's not hard once you grasped the idea, and I found using groovy swing builder and Miglayout is easy to create quite sophisticated GUI.

I wrote my experiences in 3 blog posts here.

https://sites.google.com/site/dracodoc/groovy/swingbuildermiglayout

dracodoc
  • 2,603
  • 1
  • 23
  • 33