The Vaadin Wiki is temporarily in read only mode due to large recent spam attacks.

Vaadin 7

Using font icons

WARNING: This wiki page was last edited over a year ago and might be outdated.

Font icons in Vaadin 7.2 #

A “font icon” is an icon that is a glyph (essentially a character) from a font. A font that is made for this purpose (containing only icons) is called an “icon font”. Font icons scale well (being vectors), so you do not have to make icons for a specific pixel size. Icon fonts are also typically very light-weight (in bytes), and easy to use once set up.

Vaadin 7.2 comes with generic support for font icons, and has the popular FontAwesome icon font built-in. The support is ‘generic’, in the sense that it does not include some of the advanced styling options specifically available in FontAwesome (such as spinning icons). 

FontAwesome is included in the theme by default, but the fonts are only loaded by the browser when used. If needed, you can also remove the CSS, minimize the font, or make a custom font.

A demo application showing the result is available on GitHub (java, scss)

Using FontAwesome #

You can use font icons with the familiar setIcon() method. It looks like this, when using the built-in FontAwesome:

    button.setIcon(FontAwesome.BOOKMARK);

You can also easily embed an icon in any place that allows HTML: 

    label.setContentMode(ContentMode.HTML);
    label.setValue(“Press “ + FontAwesome.BOOKMARK.getHtml() + “ to bookmark”);

Making a custom set of icons #

There are many tools for making icons, and icon fonts. One of our favourite tools is IcoMoon, which is an excellent online application where you can pick icons from a vast library of ready-made icon fonts, to compose your own set of icons. When you’re done, you can download a zip that contains your font files - just copy the ‘fonts’ folder to your theme.

  1. Browse to http://icomoon.io/app 
  2. ‘Add icons from library’ (the first time, some icons sets are added automatically) 
  3. ‘Add’ the icon set(s) you fancy (notice the licenses)
  4. Mark the icons you want in your font
  5. ‘Download’ the font

In IcoMoon you can also produce a customised version of FontAwesome, if you for instance want to remove unused icons, or swap individual icons. You can also re-upload your custom icon font, if you want to make changes or additions to it later. 

  1. ‘Import icons’
  2. Add/delete/swap icons
  3. ‘Download’ the new font

When using ready-made icons, please pay attention to the licenses the different icon sets have.

Using a custom icon font #

To use your own icon set, you need to do four things: 

  1. Make an icon font 
  2. Make it easily usable from Java and 
  3. Load the font in your theme 
  4. Use the icons in your application.

(You can skip #1 if you already have an icon font you want to use.)

1. Compose an icon font in e.g IcoMoon, download, and copy the “fonts” folder from the zip to your theme.

2. Add the following to your styles.scss OUTSIDE of the .mytheme { } block:

@include font(IcoMoon, '../../fonticondemo/fonts/icomoon');

The first parameter, “IcoMoon”, is the font-family - i.e the name you want to use for your font. You can choose anything, as long as you use the same name in Java later.
The second parameter is the filename base; in this case the files are called icomoon.ttf, icomoon.svg, etc...

3. Make use of the icons in Java; you can make an anonymous FontIcon instance, but in practice you will probably want to make an enum or some sort of icon factory. The FontAwesome implementation uses an enum, in this manner:

        private final int codepoint;

        IcoMoon(int codepoint) {
            this.codepoint = codepoint;
        }

        @Override
        public String getFontFamily() {
            // This must match the name you use in your (S)CSS
            return "IcoMoon";
        }

        @Override
        public int getCodepoint() {
            return codepoint;\\
        }

        @Override
        public String getHtml() {
            return "<span class=\"v-icon IcoMoon\">&#x"
                    + Integer.toHexString(codepoint) + ";</span>";
        }

        @Override
        public String getMIMEType() {
            throw new UnsupportedOperationException(“Not supported for FontIcon”);
        }
    }

(Note that you can easily generate the enum from the list of icons in the zip downloaded from IcoMoon.)

4. Now you can use your icon:

    button.setIcon(IcoMoon.RIBBON);

Styling font icons #

You can not generally set style names on the icon itself, instead you apply styles to the _component_ where the icon is used, much in the same way you would style anything else in a component.

Given a button with a font icon and a style name:

    button.addStyleName("redicon");
    button.setIcon(FontAwesome.SAVE);

…you can then style the icon by applying styles to the .v-icon element:

    .redicon .v-icon {
        color: red;
        font-size: 20px;
    }

Note that the icon is actually text, so you style it in much the same way you style text. 

A font icon also gets an additional .<font-family> stylename, so you can apply styles to only font icons (not ‘regular’ image icons):

.v-button .FontAwesome {
    color: blue;
}
0 Attachments
37944 Views
Average (5 Votes)
Comments
Add Comment
Hi, where can I see what the icons behind the names like "SAVE" look like?
Posted on 6/6/14 12:14 PM.
You can see the full list here:

http://fortawesome.github.io/Font-Awesome/icons/
Posted on 6/7/14 12:35 PM in reply to Anky S..
Is it also possible to use a custom icon font if you're still using a legacy (Vaadin 6) based theme, using .css instead of .scss?
Posted on 6/11/14 11:48 AM.
Any tips on how to set the background color for a circular font, that fits inside the circle?
Posted on 4/22/15 2:00 PM.