How do you get a gwt label to fill a grid cell?
I have a gwt label and I place it into a gwt grid. I have some events on the label. So I need the label to fill up the entire grid cell... otherwise the mouse over events won't trigger.
Currently I am doing a setWidth("100%) and setHeight("100%) on the label. This gets the label to fill horizontally... but I can't get the height to fill the grid cell. How do you get the label to fill the entire grid cell (horz and vert)? isn't there some sort of CSS property I ca开发者_StackOverflow社区n change to 100%?
The farthest I got was to wrap the label in a grid. Then embed the grid/label into the grid cell. The label then looks like it takes up the entire grid. But the events still only work for the label.
To capture the event on a cell you could use set a clickhandler on the grid and check if the passed cell in the event is the label cell you need to handle. This way you can certainly have an event that is triggered for the whole cell content.
The reason the cell is not filled 100% is very likely because the table (by default) has cell spacing set, which restricts you from getting a 100% width/height. You can set the cellspacing to 0, but this means it applies to all cells in the table, and that means you layout might change in undesired ways, so having the clickhandler on the table is a better solution.
I created a custom "FillLabel" class. Then set the CSS style properties width/height to both be 100%. This custom FillLabel class seems to fulfill the requirements... Although I feel like there is a more elegant solution and I welcome any others.
I extended Grid and added the various mouse event interfaces:
import com.google.gwt.event.dom.client.*
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
public class FillLabel
extends Grid
implements HasMouseOutHandlers,
HasMouseOverHandlers,
HasMouseMoveHandlers
{
private Label label;
/** Use this constructor if you are creating a root for a new tree. */
public FillLabel(String name) {
super(1,1);
this.label = new Label(name);
this.setWidget(0, 0, this.label);
}
/***************************************************************************
* Mouse Over Events
***************************************************************************/
@Override
public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
return this.addDomHandler(handler, MouseOverEvent.getType());
}
/***************************************************************************
* Mouse Out Events
***************************************************************************/
@Override
public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
return this.addDomHandler(handler, MouseOutEvent.getType());
}
/***************************************************************************
* Mouse Move Events
***************************************************************************/
@Override
public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
return this.addDomHandler(handler, MouseMoveEvent.getType());
}
}
精彩评论