Monday, October 24, 2011

Vaadin - Including HyperLinks Inside Text

Vaadin is meant for creating Java web applications. It has built in "Link" component which is needed to include hyperlinks. It works as follows;
Link link = new Link("Click Here", 
                                 new ExternalResource("http://vaadin.com/")); 
//
But when we are using this Link component, the entire String (ie. "Click Here" in the above example), becomes the hyperlink.

But if we want something like, only the word "Here" to be the hyperlink and the "Click" to be in normal text, Vaadin has no solution for it. Vaadin doesn't have a component to use for that.


Left side hyperlink is created using "Link" component and the right side hyperlink is created using "CustomLyout"

To include that kind of a hyperlink we have to use Vaadin "CustomLayout" component. Here is how to create a hyperlink using CustomLayout.

With the CustomLayout component we can create Layouts as a template in XHTML.
String text = "Click " + "<a href='http://vaadin.com/'>" + "Here" + "</a>";
CustomLayout customLayout = new CustomLayout(new ByteArrayInputStream(text.getBytes()));
//

Further we can add styles to the above created CustomLayout, just as we are doing for other Vaadin UI Components.

No comments: