1

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.

3
  • 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? Commented Mar 31, 2011 at 11:52
  • No, I just dont want to repeat always setStyleName :-) Commented Mar 31, 2011 at 12:57
  • 1
    In that case subclassing is perfectly alright since you are not going to override render methods which could potentially break browser compatibility. Commented Mar 31, 2011 at 13:02

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's what I thought. Good someone confirm it :)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.