2016-04-25 24 views
0

我想保存在DB的按钮图标串,我想有这样的:如何设置按钮FontAwesome图标与vaadin

Button button = new Button(); 
String icon = "FontAwesome.COGS"; 
button.setIcon(icon); 

Button button = new Button(); 
String icon = "fonticon://FontAwesome/f013"; 
button.setIcon(new ThemeResource(icon)); 

什么是正确的方式为了达成这个?

回答

0

我用下面的代码

button.setIcon(FontAwesomeUtil.fromName("COG")); 

public class FontAwesomeUtil { 
    public static FontAwesome fromName(String name) { 
     FontAwesome[] arr = FontAwesome.values(); 
     int len = arr.length; 

     for(int i = 0; i < len; ++i) { 
      FontAwesome f = arr[i]; 
      if(f.name().equals(name)) { 
       return f; 
      } 
     } 

     System.out.println("name " + name + " not found in FontAwesome"); 
     return null; 
    } 
} 
+0

将名称和FontAwesome对象之间的映射存储在HashMap中一次并在之后在地图上工作会更高效。 –

2

存储在数据库中的图标的名称,解决了这个和名字加载:

setIcon(FontAwesome.valueOf("COGS")) 

这可以失败,并ClassNotFoundException

+0

应标记为答案。 –

相关问题