Reputation: 6292
I have a question related to custom GWT widget.
I am going to make a custom ListBox which performs language selection. I tried to achieve this by extending the GWT's ListBox class and it works.
public class LanguageListBox extends ListBox {
....
}
However, I think from a Java point of view, we might prefer composite over inheritance. I do read some articles that suggests extending Composite rather than the widget itself.
public class LanguageListBox extends Widget {
private final ListBox listBox;
LanguageListBox() {
listBox = new ListBox();
}
......
}
This class does not work. I believe I failed to create bridging method that forward invocation to certain method of LanguageListBox to the wrapped ListBox. However, I have checked there are numerous of methods of ListBox, which method do I need to forward in order to make this design working?
All in all, I am not sure what is the best design in this scenario.
Upvotes: 0
Views: 647
Reputation: 12136
Even though composition is generally prefered over inheritence. In this case I would definitely go with inheritence as you basically want to add new or customize existing functionality in the widget while preserving almost everything ListBox
has to offer. I think that generally when dealing with GUI components, the inheritence is usual and, in most cases, the best solution.
However you should make sure, that you cannot achieve the same effect with normal ListBox
without inheriting it.
As you mentioned that class will not work as there is absolutely no bridge between ListBox
and LanguageListBox
. It does not make much sense.
As for language selection. I think it can be achieved with normal ListBox
unless you want to have some very special functionality.
Upvotes: 2