Im starting to work with GWT and build my own Button. I read about best practices and that I should extend Composite instead of Widget. But why? Here on Stackoverflow i read that the GWT Widgets have special behaviour for some browsers, but when I extend a Widget that behaviour isn't lost, is it? The point is, I want a Button, just with another style. And because I need it more than once, I dont want to repeat the code all the time. But if I extend Composite I must offer the same methods like Button to hand off things like setClickHandler(...). This looks like alot of overhead.
-
2Just wondering... Do you actually need to subclass Button for your requirement? Have you tried setStyleName/stylePrimaryName and similar CSS accessors before deciding to subclass? Why didn't it work?Ashwin Prabhu– Ashwin Prabhu2011-03-31 11:52:42 +00:00Commented Mar 31, 2011 at 11:52
-
No, I just dont want to repeat always setStyleName :-)lrxw– lrxw2011-03-31 12:57:15 +00:00Commented Mar 31, 2011 at 12:57
-
1In that case subclassing is perfectly alright since you are not going to override render methods which could potentially break browser compatibility.Ashwin Prabhu– Ashwin Prabhu2011-03-31 13:02:09 +00:00Commented Mar 31, 2011 at 13:02
Add a comment
|
2 Answers
Use Composite of you want to create complex widget. The Composite class allows you to use others existing widget inside your widget.
For your case, just subclass Button widget because you don't want add complexity to your button.
1 Comment
lrxw
Thanks, that's what I thought. Good someone confirm it :)
Extending Composite or Button (worse) is not necessary,
You can create class that extends no other class ( Button..) as follow:
public class MyButton {
Button btn= new button("btn");
VerticalPanel vpanel= new VerticalPane();/* or HorizontalPanel ..*/
/* add whatever you need */
public MyButton(){
/* add style to button and or to vpanel use btn.setStyleName("style-name") */
vpanel.add(btn)
}
public Button getButton(){ return btn; }/* it allows you get button to add ClickHandlers..*/
public Widget asWidget() { return vpanel; } /* Widget because mybe you ll need HozonalPanel */
/* you can add more features: ... */