1

I want to access JFrame(GraphicsConfiguration) using SwingBuilder.frame(), but I can't set it via the attributes, since it's unavailable. How do you pass constructor parameters using the Groovy Builders?

UPDATE: As requested, including the solution. The value parameter for the FrameFactory.newInstance(builder, name, value, attrs) method is checked first to see if it's a JFrame itself. If not, it's ignored, and a new JFrame is created. It is then passed to a post-init method to handle the attributes.

This maps to the following syntax:

builder.name(value, attrs){}

where attrs is your map of attributes in the standard key:value format.

So, to complete with an example:


SwingBuilder swing = new SwingBuilder()
// pass the title to the valueFrame, even though we can pass as attr, for the example
JFrame valueFrame = new JFrame("Value Frame Title")
JFrame myFrame = swing.frame(valueFrame, 
                             pack:true, 
                             defaultCloseOperation:JFrame.DISPOSE_ON_CLOSE) {
  ... add your panels, etc here
}
assert myFrame == valueFrame
Spencer Kormos
  • 8,381
  • 3
  • 28
  • 45

1 Answers1

2

You should be able to pass in a JFrame as the value argument, according to the SwingBuilder.frame docs; maybe try that.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Can you provide an example, because I'm not sure how this helps me. The value I provide the frame() method will just return the value, but I still need to instantiate the value with a constructor parameter. – Spencer Kormos Nov 09 '11 at 18:51
  • I guess my problem is still my inexperience with the syntax of Groovy, specifically with the builders. Where are you providing the value argument, as an attribute, or in the provided closure? – Spencer Kormos Nov 09 '11 at 19:06
  • Figured it out from other examples. Thanks. – Spencer Kormos Nov 09 '11 at 19:10
  • @Spencer can you edit your question to paste the solution in? I'm interested to know the solution... Do you have to go the path of widgets? – tim_yates Nov 09 '11 at 19:15
  • @Tim as requested. I hope it makes sense. I do know it works! – Spencer Kormos Nov 09 '11 at 19:27
  • 1
    @SpencerK I left the house at the wrong time! But you already figured it out; the class you pass in is what's used as the binding target. Glad you worked it out. – Dave Newton Nov 09 '11 at 19:56