You'll be better off using an Anchor instead of a Hyperlink - it's the
same visually, but you get to control what happens on click by adding a
ClickHandler. You should find it results in much cleaner code.
HTH
Paul
Ray wrote:
> I been assigned the task of making hyperlinks work, change color on
> hovering and clicking, and being able to enable and disable the
> links. I have the color changing part working and I thought I had the
> disable/enble working, but now I can not get the onClick to be
> reconized. Below is the code to extend the hyperlink so I can add the
> enable/disable methods.
>
> public class lawHyperlink extends Hyperlink implements
> SourcesClickEvents, HasClickHandlers {
> // define the HTML hyperlink
> private Html hyperLink;
> private final String str;
> private static HyperlinkImpl impl = GWT.create(HyperlinkImpl.class);
>
> public lawHyperlink(String s1, String s2) {
> str = s1;
> hyperLink = new Html("<span class='x-nodrag'>" + s1 + "</span>")
> {
> protected void onRender(Element target, int index) {
> super.onRender(target, index);
> el().addEventsSunk(Event.MOUSEEVENTS);
> el().addEventsSunk(Event.ONCLICK);
> }
> };
>
> enabled();
>
> } // end of lawHyperlink constructor
>
> public Html displayHyperlink() {
> return hyperLink;
> }
>
> public void disabled() {
> hyperLink.removeAllListeners();
> hyperLink.addStyleName("gwt-Hyperlink-disabled");
> hyperLink.setStyleAttribute("color", "#E0E0DA");
> } // end of disabled method
>
> public void enabled() {
> Listener<BaseEvent> listener = new Listener<BaseEvent>() {
> public void handleEvent(BaseEvent be) {
> Html h = (Html) be.getSource();
> String c = h.el().getStyleAttribute("color");
> // Info.display("Listener color before test ", c);
> c = c .equals("#ff00ff") ? "#ff0000" : "#FF00FF";
> // Info.display("Listener color after test ", c);
> h.setStyleAttribute("color", c);
> }};
>
> hyperLink.addStyleName("gwt-Hyperlink");
> hyperLink.setStyleAttribute("color", "#00ff00");
> hyperLink.addListener(Events.OnMouseOver, listener);
> hyperLink.addListener(Events.OnMouseOut, listener);
>
> } //end of enabled method
>
> @Override
> public HandlerRegistration addClickHandler(ClickHandler handler) {
> Info.display("addClickHanler called ", "from lawHyperlink");
> return addHandler(handler, ClickEvent.getType());
> } // end of addClickHandler method override
>
> @Override
> public void onBrowserEvent(Event event) {
> super.onBrowserEvent(event);
> if (DOM.eventGetType(event) == Event.ONCLICK &&
> impl.handleAsClick
> (event)) {
> History.newItem(getTargetHistoryToken());
> DOM.eventPreventDefault(event);
> }
> } // end of onBrowserEvent method override
>
>
> } // end of lawHyperlink class definition
>
> I add the onClick handler in another class that puts it on a panel
> that gets added to the rootPanel.
>
> lawHyperlink firstPage = new lawHyperlink("|< First Page",
> "FirstPage");
> firstPage.addClickHandler(new ClickHandler() {
> public void onClick(ClickEvent event) {
> MessageBox.alert("onClick", "Clicked First Page", null);
> }});
> linkPanel.add(firstPage.displayHyperlink());
>
> Everything displays fine and the trace looks good, but the onClick
> event is not being reconized. I believe I'm missing something from my
> lawHyperlink class, but I have not been able to determine what. Any
> help will be greatly appreciated.
>
> Thanks,
> Ray
>
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---