You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TableCellStyle.java 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.tests.book;
  5. import com.vaadin.ui.CustomComponent;
  6. import com.vaadin.ui.Table;
  7. public class TableCellStyle extends CustomComponent {
  8. public TableCellStyle() {
  9. Table table = new Table("Table with Cell Styles");
  10. table.addStyleName("checkerboard");
  11. // Add some columns in the table. In this example, the property IDs
  12. // of the container are integers so we can determine the column number
  13. // easily.
  14. table.addContainerProperty("0", String.class, null, "", null, null); // Row
  15. // header
  16. for (int i = 0; i < 8; i++) {
  17. table.addContainerProperty("" + (i + 1), String.class, null, String
  18. .valueOf((char) (65 + i)), null, null);
  19. }
  20. // Add some items in the table.
  21. table.addItem(new Object[] { "1", "R", "N", "B", "Q", "K", "B", "N",
  22. "R" }, new Integer(0));
  23. table.addItem(new Object[] { "2", "P", "P", "P", "P", "P", "P", "P",
  24. "P" }, new Integer(1));
  25. for (int i = 2; i < 6; i++) {
  26. table.addItem(new Object[] { String.valueOf(i + 1), "", "", "", "",
  27. "", "", "", "" }, new Integer(i));
  28. }
  29. table.addItem(new Object[] { "7", "P", "P", "P", "P", "P", "P", "P",
  30. "P" }, new Integer(6));
  31. table.addItem(new Object[] { "8", "R", "N", "B", "Q", "K", "B", "N",
  32. "R" }, new Integer(7));
  33. table.setPageLength(8);
  34. // Set cell style generator
  35. table.setCellStyleGenerator(new Table.CellStyleGenerator() {
  36. public String getStyle(Object itemId, Object propertyId) {
  37. int row = ((Integer) itemId).intValue();
  38. int col = Integer.parseInt((String) propertyId);
  39. // The first column.
  40. if (col == 0) {
  41. return "rowheader";
  42. }
  43. // Other cells.
  44. if ((row + col) % 2 == 1) {
  45. return "black";
  46. } else {
  47. return "white";
  48. }
  49. }
  50. });
  51. setCompositionRoot(table);
  52. }
  53. }