Abhaya
Abhaya

Reputation: 1

add label to check box item in smart gwt

I need to add a label to my checkbox in smart GWT. For example, I have three fruits: apple, mango and banana. How can I add the label Fruits as heading. I am also unable to use CheckboxGroup.

CheckBox check1 = new CheckBox();
check1.setBoxLabel("Classical");

CheckBox check2 = new CheckBox();
check2.setBoxLabel("Rock");
check2.setValue(true);

CheckBox check3 = new CheckBox();
check3.setBoxLabel("Blue");

CheckBoxGroup checkGroup = new CheckBoxGroup();
checkGroup.setFieldLabel("Music");
checkGroup.add(check1);
checkGroup.add(check2);
checkGroup.add(check3);

Upvotes: 0

Views: 1243

Answers (1)

dev_in_progress
dev_in_progress

Reputation: 2494

Hy Abhaya, first of all i don't know what version of smart GWT you use but i can see that this is not smart GWT, its more like gwt component and that CheckBoxGroup is some awt component.

If You want to use smart GWT check box component then use CheckboxItem widget, if its item that means you need to use dynamic form.

So lets say:

import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.DynamicForm;

CheckBoxItem item1 = new CheckBoxItem();
item1.setTitle("Classical");

CheckBoxItem item2 = new CheckBoxItem();
item1.setTitle("Rock");

So now we have 2 check box items, wee need to put them in dynamic form like this (I set border so u can see what happens):

DynamicForm box = new DynamicForm();
box.setBorder("1pt solid red")
box.setFields(item1,item2);

Lets draw that:

box.draw();

This is output: drawn dynamic form

Ok now we need title, best way is to add lable before dynamic form. Now dynamic form is root element, lets put it in some layout (new package), layout will be vertical so for every new member it will be one beneath other:

import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.Label;

Label title = new Label();
title.setContents("Music");
title.setHeight(20);

VLayout lay = new VLayout();
lay.setBorder("1pt solid black");
lay.addMember(title);
lay.addMember(box);

So wee added title in layout then dynamic form named "box", now u don't need to draw() box, only layout because that layout consists of label and box that will be automatically drawn:

lay.draw();

And output is this: drawn vertical layout

Here is the little bit styled look: Styled look

Entire code:

    CheckboxItem item1 = new CheckboxItem();
    item1.setTitle("Classical");

    CheckboxItem item2 = new CheckboxItem();
    item2.setTitle("Rock");

    CheckboxItem item3 = new CheckboxItem();
    item3.setTitle("Metal");

    CheckboxItem item4 = new CheckboxItem();
    item4.setTitle("House");

    DynamicForm box = new DynamicForm();
    box.setFields(item1,item2, item3, item4);
    box.setWidth100();
    box.setHeight100();
    box.draw();

    Label title = new Label();
    title.setContents("<h3>Music:</h3>");
    title.setHeight(20);


    VLayout lay = new VLayout();
    lay.setMargin(20);
    lay.setBorder("1pt solid black");
    lay.setPadding(10);
    lay.setBackgroundColor("#e6e6e6");
    lay.addMember(title);
    lay.addMember(box);
    lay.draw();

In property setContents() u can use HTML tags and CSS

I hope it helps. Regards, Jakov A.

Upvotes: 1

Related Questions