1
votes

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.

2
Just 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
No, I just dont want to repeat always setStyleName :-)lrxw
In that case subclassing is perfectly alright since you are not going to override render methods which could potentially break browser compatibility.Ashwin Prabhu

2 Answers

4
votes

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
votes

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: ... */