Adding icons to an button in gwt

允我心安 提交于 2019-12-11 11:52:32

问题


i have button and want to set an icon to it along with the text inside it. their is no property to add icon to button like in smartGwt .. any idea how to achieve it please help.


回答1:


There are many ways of achieving this.

Way 1 : Easy way

Just set the background image via code. myButton.getElement().getStyle().setBackgroundImage("path");

Way 2: Another easy way

Set your own html myButton.setHtml("Pass the html string");

Way 3: Easy but gives more control

myButton.addStylename("buttonStyle")

Use css to style this

.buttonStyle{
color : red;
}

Way 4: Best way according to me

Create your own split button wrapping it around a flowpanel or horizontalPanel, with image as your first widget and button as your another widget. This gives you additional control on image and as well as button. You can have your click handler on image as well as button and you can style each one of them individually.




回答2:


This is how I achieved setting an icon in my get:button.

Add an extra style class hook, mine below is btn-fa-group to your gwt button. If you use the attribute 'addStyleNames' you can define them in your stylesheet and have multiple classes.

<g:Button text=" Post Your Answer" enabled="false" ui:field="showPostButton" addStyleNames="btn btn-default btn-fa-group" />

Now in your CSS define the following declaration:

btn-fa-group:before {
    color: #333333;
    content: "\f0c0";
    display: inline-block;
    font-family: "fontawesome";
}

Some important things to note; don't forget the before selector, make sure the unicode starts with a slash and have fontAwesome installed. Alternatively you can use another glyph icon if you have the font installed.




回答3:


You can set innerhtml with image in button i.e.

 Button button=new Button("<image src='abc.jpg' width='200px' height='300px' />Ok");



回答4:


Button bt = new Button();

bt.getElement().getStyle().setBackgroundImage("url('path/to/ur/image/imagename.extention')");

also set size of background image wrt to the size of button

bt.getElement().getStyle().setProperty("backgroundSize","30px");



回答5:


Add a Css Class to your Button is probalby the best solution.

button.addStyleName("ButtonIcon");

How to define the CSS and HTML you can read here.




回答6:


Yes ,you can .Gwt have a SmartGwt type button called push buttopn

com.google.gwt.user.client.ui.PushButton

You can pass Image object to it as below

Image  image = new Image(GWT.getModuleBaseURL() + "/images/search-arrow.png");
RootPanel.get().add(new PushButton(image));


来源:https://stackoverflow.com/questions/14743492/adding-icons-to-an-button-in-gwt

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