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?
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