2016-04-25 76 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

應標記爲答案。 –