Align the cells' content in a Grid in gwt
css, gwt, java
Solution
There are methods in CellFormatter class `setHorizontalAlignment()` and `setVerticalAlignment()`:
Grid grid = new Grid();
HTMLTable.CellFormatter formatter = grid.getCellFormatter();
formatter.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_CENTER);
formatter.setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_MIDDLE);
in your css for as a replacement of that method `setVerticalAlignment()` use: `vertical-align:middle;` and for `setHorizontalAlignment()` use td align attribute
Problem
I am trying to align the content of the Grid-cells in a Grid(1,4). Each cell contains a ListBox. I apply CSS to each column and althougn I can adjust the width I cannot manage to align the ListBoxes in the center. The ListBoxes do not move to the center of the cells even if I apply CSS directly on them instead of the cells. !important does not change anything anything. What could be wrong? Here is the relevant code: ``` Grid mYGrid = new Grid(1, 4); mYGrid.addStyleName("mYgrid"); mYGrid.getCellFormatter().setWidth(0, 0, "150"); mYGrid.getCellFormatter().setWidth(0, 3, "150"); mYGrid.setWidget(0, 1, getMonthsList() ); mYGrid.setWidget(0, 2, getYearsList() ); mYGrid.getColumnFormatter().setStyleName(1,"monthsCell"); mYGrid.getColumnFormatter().setStyleName(2,"yearsCell"); ``` CSS: ``` .monthsCell{ width:200px; margin-left: auto; margin-right: auto; ``` } ``` .yearsCell{ width:200px; margin-left: auto !important; margin-right: auto !important; ``` }