Creating a new Custombutton by subclassing a Button

68 views
Skip to first unread message

javed ansari

unread,
Jul 19, 2012, 6:46:38 AM7/19/12
to Google Web Toolkit
I have created one CustomButton which consists of once image and one
label.

public class CustomButton extends Button implements ClickHandler{
// call an empty constructor
public ConfigButton() {
}

private Label conflbl;
private Button btn;

public CustomButton(String imgUrl, String btnText) {
this.parentScreen = parentScreen;
Image img = new Image();
img.setUrl(imgUrl);
img.setSize("20px", "20px");
AbsolutePanel panel = new AbsoultePanel();
panel.add(img, nleft + 110, ntop - 30);
btn = new Button();
btn.setSize("250px", "300px");
conflbl = new Label(btnText);
conflbl.setSize("130", "60");
panel.add(btn, nleft, ntop);
panel.add(conflbl, nleft + 60, ntop + 40);
conflbl.addClickHandler(this);
btn.addClickHandler(this);
}
@Override
public void onClick(ClickEvent event) {
// Handle the label and the button click here.
// Then raise a fresh Click handler. But How???
this.Click() // This is not working

}
}

I want to raise a click event once this button is clicked and
obviously in the OnClick() handler of the CustomButton should receive
Source as the CustomButton (Not as Button). This way our CustomButton
will work truly as a button. I tried calling this.Click() but it is
not raising the event.

Joseph Lust

unread,
Jul 19, 2012, 9:11:52 AM7/19/12
to google-we...@googlegroups.com
Your issue is "this.Click()." In Java, method names are not capitalized, so I would not expect this method to exist. You want Button.click().



Sincerely,
Joseph

javed ansari

unread,
Jul 20, 2012, 12:30:50 AM7/20/12
to google-we...@googlegroups.com
Sorry for the typo. There is no method like Click(). I am calling this.click() only. But that does not work.

I am thinking to write my own customClick event and raises from my CustomButton.

Andrea Boscolo

unread,
Jul 20, 2012, 5:59:47 AM7/20/12
to google-we...@googlegroups.com
I don't get why extending Button and also declare a Button as a field.
Joseph is right: call click() but pay attention on what, this.click() calls the click on the CustomButton instance, btn.click() calls the click on the inner declared Button field. As I say, I don't get having two of them.

Anyway take a look at TextButton and CustomButtn in GWT. I believe they can be easily used to achieve an image+text button.
Probably the quicker way is defining a simple image+text SafeHtmlTemplate and pass it to the Button(SafeHtml) constructor.
Choose you poison :)
Reply all
Reply to author
Forward
0 new messages