Setting image dynamically in ImageHyperLink

I have come across this question several times, in forums as well as other support channels.  Posting it on the NetBeans support blog, hopefully might help others. 

Here we are referring to a Visual Web Project, created by selecting from main menu  File -> New Project  -> Web Application -> Visual Web JavaServer Faces. On creating the project, you will get the visual designer page. Now you can create the Image Hyperlink component by simply dragging and dropping the component on to the visual designer from the palette. To set the image for the image HyperLink component you would then set the imageURL property in the properties window as shown below in the screen shot.


However,  if you want to do this dynamically, you need to add the required code. The common error message reported on trying to attempt this is:  "runtime error : duplicate component Id _image". You will get this error message if you do not have the setId()  method on the image Hyperlink component, to ensure that it is a unique id.

Below is a simple code snippet to do the same. I used Grid Panel layout, and then added the following code to the Java source.

public HtmlPanelGrid getGridPanel1() {
ImageHyperlink hyperLink1 = createHyperlink("link1");
ImageHyperlink hyperLink2 = createHyperlink("link2");
gridPanel1.getChildren().add(hyperLink1);
gridPanel1.getChildren().add(hyperLink2);
return gridPanel1;
}

private ImageHyperlink createHyperlink(String Id) {
//Create the hyperlink
ImageHyperlink navigationLink = null;
navigationLink = new ImageHyperlink();
navigationLink.setId(Id);
navigationLink.setImageURL("/resources/help.gif");
return navigationLink;
}

Final note, do not forget to Fix imports (right click in editor -> Fix Imports), to make sure the import statements are added for any components you added manually to your Java source file.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

A blog for NetBeans users maintained by NetBeans Support team. For professional support, please check out Support Resources.

Search

Archives
« February 2015
SunMonTueWedThuFriSat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
       
       
Today