开发者

GWT - extending Composite instead of Widget

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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜