Skip to content

7. How to create table

Web Firm Framework edited this page Aug 19, 2017 · 3 revisions

See the below sample to create a table

public static void main(String[] args) {

	class Employee {

		private long id;

		private String firstName;

		private String lastName;

		public Employee(long id, String firstName, String lastName) {
			this.id = id;
			this.firstName = firstName;
			this.lastName = lastName;
		}
		//getters and setters
	}

	Employee employee1 = new Employee(1, "Kousuke", "Furuta");
	Employee employee2 = new Employee(2, "Shunsuke", "Kimoto");
	Employee employee3 = new Employee(3, "Yoshiko", "Kaya");
	Employee employee4 = new Employee(4, "Lacey", "Koizumi");
	Employee employee5 = new Employee(5, "Yakira", "Horie");

	final List<Employee> employees = new ArrayList<Employee>();
	employees.add(employee1);
	employees.add(employee2);
	employees.add(employee3);
	employees.add(employee4);
	employees.add(employee5);

	Html html = new Html(null) {
		Body body = new Body(this) {
			Table table = new Table(this) {

				{
					for (final Employee employee : employees) {
						Tr tr = new Tr(this) {
							Td td1 = new Td(this) {
								Blank cellContent = new Blank(this,
										String.valueOf(employee.getId()));
							};
							Td td2 = new Td(this) {
								Blank cellContent = new Blank(this,
										employee.getFirstName());
							};
							Td td3 = new Td(this) {
								Blank cellContent = new Blank(this,
										employee.getLastName());
							};
						};
					}
				}

			};
		};
	};

	html.setPrependDocType(true);
	System.out.println(html.toHtmlString(true));

}

prints

<!DOCTYPE html>
<html>
    <body>
        <table>
            <tr>
                <td>1</td>
                <td>Kousuke</td>
                <td>Furuta</td>
            </tr>
            <tr>
                <td>2</td>
                <td>Shunsuke</td>
                <td>Kimoto</td>
            </tr>
            <tr>
                <td>3</td>
                <td>Yoshiko</td>
                <td>Kaya</td>
            </tr>
            <tr>
                <td>4</td>
                <td>Lacey</td>
                <td>Koizumi</td>
            </tr>
            <tr>
                <td>5</td>
                <td>Yakira</td>
                <td>Horie</td>
            </tr>
        </table>
    </body>
</html>
(Note: The generated html will be minified, here we formatted it for readability)