Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help to display an icon image in a command button

Just a basic need but cannot get it to work. I would like to have a primefaces button to display my image, without any text.

But what i get is a button that contains only a ^ character and does NOT displaying the image, which only has a size of 16x16.


So, this is the primefaces button :

<p:commandButton image="ui-icon-csv" title="CSV Document" ajax="false">
    <p:dataExporter type="csv" target="gridRPBDetails" 
        fileName="#{tInputBean.exportFilename}" />
</p:commandButton>

This is the css file :

.ui-icon-csv {
    background-image: url(images/csv_small.png);
}

And this is the generated html for the button :

<button type="submit" onclick=";" 
    name="gridRPBDetails:j_idt82" id="gridRPBDetails:j_idt82" 
    class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-primary" 
    role="button" aria-disabled="false">
        <span class="ui-button-icon-primary ui-icon ui-icon-csv"></span><span class="ui-button-text">CSV Document</span>
</button>

And to prove the image accessible, i try this URL, and indeed it shows the picture :

http://albert:8080/mywebapp/faces/javax.faces.resource/images/csv_small.png

Im using tomcat 7, and these are my dependencies :

<dependency>
    <groupId>org.primefaces</groupId>
    <artifactId>primefaces</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.0.4-b09</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.0.4-b09</version>
    <scope>compile</scope>
</dependency>

Any ideas what went wrong ?

Thank you !

like image 374
Albert Gan Avatar asked May 11 '11 05:05

Albert Gan


4 Answers

After some investigation using firebug, i found out that the culprit is from the CSS stuff i defined.

So these are the changes i made to make this work.


The JSF button :

<p:commandButton image="ui-icon-xls" title="Excel Document" ajax="false">
    <p:dataExporter type="xls" target="gridRPBDetails" 
        fileName="#{tInputBean.exportFilename}" />
</p:commandButton>

The CSS :

.ui-state-default .ui-icon-xls {
    background-image: url(images/excel_small.png);
}

And here is the generated html :

<button type="submit" title="Excel Document" onclick=";" 
  name="gridRPBDetails:j_idt81" id="gridRPBDetails:j_idt81" 
  class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only" 
  role="button" aria-disabled="false">
    <span class="ui-button-icon-primary ui-icon ui-icon-xls"></span>
    <span class="ui-button-text">ui-button</span>
</button>

Thank you !

like image 94
Albert Gan Avatar answered Nov 10 '22 21:11

Albert Gan


write ui-widget before the icon class name

.ui-widget .ui-icon-xls {
    background-image: url(images/excel_small.png);
}

the .ui-state-default will not make it visible in other components like <p:menuitem/> and <p:menuButton/>

like image 43
Talaat Safwat Avatar answered Nov 10 '22 21:11

Talaat Safwat


Primefaces provide the graphicImage as shown in their showcase-labs (broken link):

<p:commandLink ajax="false">  
    <p:graphicImage value="/images/excel.png" />  
    <p:dataExporter type="xls" target="tbl" fileName="cars" />  
</p:commandLink>  

It can be useful in someway to someone.


* For PrimeFaces 5.2, follow to this showcase-labs link. Note that, in that case, the above code wouldn't work.

like image 34
falsarella Avatar answered Nov 10 '22 22:11

falsarella


When commandButton is used, is important that the png file size be 16 x 16, because a bigger image might no be displayed correctly, or not displayed at all.

like image 1
user2351270 Avatar answered Nov 10 '22 22:11

user2351270