PDA

View Full Version : Determining if an icon has been clicked in a button



NotChris
10 Mar 2011, 12:56 PM
I am wondering how to find out if the image within a button has been clicked on. I have tried the suggested approaches in:

http://www.sencha.com/forum/showthread.php?95258-Clickable-Header-icon&highlight=abstractimageprototype+click (http://www.sencha.com/forum/showthread.php?95258-Clickable-Header-icon&highlight=abstractimageprototype+click)

with no luck. I have a button defined with an image for it using:


myBtn.setIcon(AbstractImagePrototype.create(ImagesResources.INSTANCE.addMyIcon()));
@Source("myicon.gif")
ImageResource addMyIcon();



I guess my question is basically the last post in that forum (if that is what will return the clicked icon) - How to check the target of the click is the ICON?

Thank you.

bogusky
11 Mar 2011, 2:41 AM
public class GXTTest implements EntryPoint {
Button btn;
public void onModuleLoad() {
ContentPanel main = new ContentPanel();
main.setSize(400, 300);
main.setHeading("This is panel heading");
Header header = main.getHeader();
header.setIcon(GXT.IMAGES.paging_toolbar_refresh());

final Listener listener = new Listener() {
public void handleEvent(BaseEvent be) {

ComponentEvent ce = (ComponentEvent)be;
El el = El.fly(ce.getTarget(), "component");
String elStyle = el.getStyleName();
if (elStyle != null && elStyle.contains("x-panel-inline-icon")) {
MessageBox.info("", "Click on header's icon!", null);
}
}
};
header.addListener(Events.OnClick, listener);
header.sinkEvents(Event.ONCLICK);

btn = new Button("This is button.", GXT.IMAGES.paging_toolbar_refresh()) {
@Override
protected void onClick(ComponentEvent ce) {
El icon = buttonEl.selectNode("." + baseStyle + "-image");
Region ir = icon.getRegion();
if (IconAlign.LEFT.equals(btn.getIconAlign())) {
if (ce.getClientX() < ir.right) {
MessageBox.info("", "Click on button's icon!", null);
}
}
// similar other IconAlign
super.onClick(ce);
}
};

main.add(btn);
RootPanel.get().add(main);
}
}

NotChris
11 Mar 2011, 6:16 AM
Thanks. It works like a charm!!