Reputation: 7067
I want to re-use a Builder instance to create several controls but I'm getting compile errors which I don't understand. I think they're caused by me not understanding generics properly. Can anyone help?
ButtonBuilder builder = ButtonBuilder.create().minHeight(40.0).minWidth(40.0);
Button button1 = builder.onAction(clickHandler).build(); // Error
Button button2 = ButtonBuilder.create().minHeight(40.0).minWidth(40.0).onAction(clickHandler).build(); //OK
where clickHandler is a EventHandler<ActionEvent>
The error I get is
error: cannot find symbol
Button button1 = builder.onAction(clickHandler).build();
^
symbol: method build()
location: class ButtonBaseBuilder
ButtonBaseBuilder implements Builder so it should have a build() method, shouldn't it? Also if I run everything together (as in button2) it is OK.
This is on JDK 7.0u4 and JavaFX SDK 2.1
Thanks in advance, Pete
Upvotes: 2
Views: 813
Reputation: 34508
You need to pass ButtonBuilder generic parameter to use builders that way:
ButtonBuilder<? extends ButtonBuilder> builder =
ButtonBuilder.create().minHeight(40.0).minWidth(40.0);
Button button1 = builder.onAction(clickHandler).build();
Upvotes: 2
Reputation: 328639
ButtonBaseBuilder
does not have a build
method. ButtonBuilder
, which extends ButtonBaseBuilder
, has one because it implements Builder<Button>
.
This should work:
Button button1 = ((ButtonBuilder) builder.onAction(clickHandler)).build();
Upvotes: 2