Image in GWT menubar instead of text

泪湿孤枕 提交于 2020-01-05 05:10:11

问题


I am using MenuBar from GWT showcase. I want to add text in the menubar. Can you please let me know that in which way I can get it ?


回答1:


You can try this one also. Its given in MenuBar Docs API

public MenuItem addItem(java.lang.String text,
                        boolean asHTML,
                        MenuBar popup)

So, In your case,

final String image = "<img src='"+GWT.getModuleBaseURL() + "/images/down-arrow.png' height='25px' width='25px'/>";


 HomeMenu.addItem(new MenuItem(image,true,mainMenu));



回答2:


The best approach is not to hack anything. Just use the Image class:

MenuItem profileMenuItem = new MenuItem(new Image(TripImages.INSTANCE.getUserSmall()) + " Profile", true, profileMenuDropdown);

Works for me.




回答3:


I have found the solution. Maybe it will help somebody...

MenuBar HomeMenu = new MenuBar();
final String image = "<img src='"+GWT.getModuleBaseURL() + "/images/down-arrow.png' height='25px' width='25px'/>";

SafeHtml addActivityImagePath = new SafeHtml() {
    @Override
    public String asString() {
        return image;
     }
 };

HomeMenu.addItem(new MenuItem(addActivityImagePath,mainMenu));



回答4:


If you're using bundle and ImageResource, you can use AbstractImagePrototype.

If GlobalBundle.INSTANCE.home() is my ImageResource, this makes it appear on the menu:

menu.addItem(AbstractImagePrototype.create(GlobalBundle.INSTANCE.home()).getSafeHtml(), ...);



回答5:


Here a link to a simple example of creating menubar. And here is an example of creating a menu with submenus.




回答6:


    MenuBar HomeMenu = new MenuBar();
final String image = "<img src='"+GWT.getModuleBaseURL() + "/images/down-arrow.png' height='25px' width='25px'/>";

SafeHtml addActivityImagePath = new SafeHtml() {
    @Override
    public String asString() {
        return image;
     }
 };

HomeMenu.addItem(new MenuItem(addActivityImagePath,mainMenu));


来源:https://stackoverflow.com/questions/9903738/image-in-gwt-menubar-instead-of-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!