Setting a style for a single cell of a celltable

2,736 views
Skip to first unread message

Rike255

unread,
Sep 20, 2011, 11:41:04 AM9/20/11
to Google Web Toolkit
Hey all,

So I have a celltable that contains a few textcolumns. I want to be
able to set a style for individual cells based on its contents.

I can set a style for a specific column by calling something like:
cellTable.getColumn(index).setStyleNames(style.disabled());

The above works but I can't figure out how to only apply a style to
one (or more) cells.

Thanks in advance!
Ryan

Sudhakar Abraham

unread,
Sep 21, 2011, 8:21:01 AM9/21/11
to Google Web Toolkit
You can set a style for individual cells of a cell table . Override
the getCellStyleNames() in your Column<T,C> class.

S. Abraham
www.DataStoreGwt.com
Persist objects directly in GAE

ozgur aydinli

unread,
Sep 21, 2011, 9:32:51 AM9/21/11
to Google Web Toolkit
Hi,

You can develop a custom cell. And in the render method do something
like below:

interface Template extends SafeHtmlTemplates {
@Template("<div class=\"{0}\">{1}</div>")
SafeHtml html(String className, String value);
}

Template template = GWT.create(Template.class);

@Override
public void render(.......){
if(some condition is true){
sb.append(template.html("class name", "cell value");

Rike255

unread,
Sep 22, 2011, 1:44:37 PM9/22/11
to Google Web Toolkit
Thanks for the responses. I'm very new to GWT/Java so I'm not too
sure what you mean by override getCellStyleNames(). Is there an
example of this (or maybe a good place for a quick explanation)?

Thanks again!

On Sep 21, 6:21 am, Sudhakar Abraham <s.abra...@datastoregwt.com>
wrote:

Rike255

unread,
Sep 23, 2011, 5:19:57 PM9/23/11
to google-we...@googlegroups.com
So I have a skeleton that looks like this:

TextColumn<StatusRpcBean> statusColumn = new TextColumn<StatusRpcBean>() {
@Override
public String getCellStyleNames(Context context, StatusRpcBean object) {
return ???;
}
@Override
public String getValue(StatusRpcBean object) {
return String.valueOf(object.getStatus());
}
};

Problem is, I really don't know what I'm looking at, I'm searching around trying to find an explanation of what's happening here but no luck yet.  What is the "Context"?  Lets say each cell in this statusColumn will hold one of two strings "Available" and "Busy" and I want to make all the "Busy" cells red (with a style), how do I override getCellStyleNames to do something like that?

Thanks again,
Ryan

Cham747

unread,
Nov 8, 2012, 2:07:48 AM11/8/12
to Google-We...@googlegroups.com
Rike255 <rgroten@...> writes:

>
>
> So I have a skeleton that looks like this:
>
>
> TextColumn<StatusRpcBean> statusColumn = new TextColumn<StatusRpcBean>() {
>
> <at> Override
>
> public String getCellStyleNames(Context context, StatusRpcBean object) {
>
> return ???;
>
> }
>
>
> <at> Override
>
> public String getValue(StatusRpcBean object) {
>
> return String.valueOf(object.getStatus());
>
> }
> };
>
>
> Problem is, I really don't know what I'm looking at, I'm searching around
trying to find an explanation of what's happening here but no luck yet.  What is
the "Context"?  Lets say each cell in this statusColumn will hold one of two
strings "Available" and "Busy" and I want to make all the "Busy" cells red (with
a style), how do I override getCellStyleNames to do something like that?
>
> Thanks again,
> Ryan

----------------

The same problem occurred to me and by lot of search and trial and error
things I got the following solution. At first it was annoying
since I'm also new to GWT.

Since your are using CellTable create a custom cell by overriding the render
method of AbractCell. This way each time the table render you will be able to
customize the render logic.

AbstractCell cell = new AbstractCell<String>() {
@Override
public void render(com.google.gwt.cell.client.Cell.Context context, String
value, SafeHtmlBuilder sb) {
if (value == null) {
return;
}

String style = "style='color:" + (Double.parseDouble(value)
< 0 ? "red" :"green") + "'";
sb.appendHtmlConstant("<span " + style + ">" + value + "</span>");
}
};

Then create the required column by using this cell.

Column<ColumnRecordType, String> orderValueColumn = new
Column<ColumnRecordType, String>(cell) {
@Override
public String getValue(ColumnRecordType record) {
return String.valueOf(record.getMyValue());
}
};

Now you can add the Column to CellTable.
Hope this help you or future guys.





Reply all
Reply to author
Forward
0 new messages