The HTMLList classes allow you to easily create lists within your HTML pages. These classes provide methods to get and set various attributes of the lists and the items within the lists.
In particular, the parent class HTMLList provides a method to produce a compact list that displays items in as small a vertical space as possible.
Use the subclasses of HTMLList and HTMLListItem to create your HTML lists:
For coding snippets, see the following examples:
Use the OrderedList and OrderedListItem classes to create ordered lists in your HTML pages.
By using the methods in OrderedListItem, you can override the numbering and type for a specific item in the list.
See the example for creating ordered lists.
Use the UnorderedList and UnorderedListItem classes to create unordered lists in your HTML pages.
See the example for creating unordered lists.
The following examples show you how to use the HTMLList classes to create ordered lists, unordered lists, and nested lists.
Example: Creating ordered lists
The following example creates an ordered list:
// Create an OrderedList. OrderedList oList = new OrderedList(HTMLConstants.SMALL_ROMAN); // Create the OrderedListItems. OrderedListItem listItem1 = new OrderedListItem(); OrderedListItem listItem2 = new OrderedListItem(); // Set the data in the OrderedListItems. listItem1.setItemData(new HTMLText("First item")); listItem2.setItemData(new HTMLText("Second item")); // Add the list items to the OrderedList. oList.addListItem(listItem1); oList.addListItem(listItem2); System.out.println(oList.getTag());
The previous example produces the following tags:
<ol type="i"> <li>First item</li> <li>Second item</li> </ol>
Example: Creating unordered lists
The following example creates an unordered list:
// Create an UnorderedList. UnorderedList uList = new UnorderedList(HTMLConstants.SQUARE); // Create the UnorderedListItems. UnorderedListItem listItem1 = new UnorderedListItem(); UnorderedListItem listItem2 = new UnorderedListItem(); // Set the data in the UnorderedListItems. listItem1.setItemData(new HTMLText("First item")); listItem2.setItemData(new HTMLText("Second item")); // Add the list items to the UnorderedList. uList.addListItem(listItem1); uList.addListItem(listItem2); System.out.println(uList.getTag());
The previous example produces the following tags:
<ul type="square"> <li>First item</li> <li>Second item</li> </ul>
Example: Creating nested lists
The following example creates a nested list:
// Create an UnorderedList. UnorderedList uList = new UnorderedList(HTMLConstants.SQUARE); // Create and set the data for UnorderedListItems. UnorderedListItem listItem1 = new UnorderedListItem(); UnorderedListItem listItem2 = new UnorderedListItem(); listItem1.setItemData(new HTMLText("First item")); listItem2.setItemData(new HTMLText("Second item")); // Add the list items to the UnorderedList. uList.addListItem(listItem1); uList.addListItem(listItem2); // Create an OrderedList. OrderedList oList = new OrderedList(HTMLConstants.SMALL_ROMAN); // Create the OrderedListItems. OrderedListItem listItem1 = new OrderedListItem(); OrderedListItem listItem2 = new OrderedListItem(); OrderedListItem listItem3 = new OrderedListItem(); // Set the data in the OrderedListItems. listItem1.setItemData(new HTMLText("First item")); listItem2.setItemData(new HTMLText("Second item")); listItem3.setItemData(new HTMLText("Third item")); // Add the list items to the OrderedList. oList.addListItem(listItem1); oList.addListItem(listItem2); // Add (nest) the unordered list to OrderedListItem2 oList.addList(uList); // Add another OrderedListItem to the OrderedList // after the nested UnorderedList. oList.addListItem(listItem3); System.out.println(oList.getTag());
The previous example produces the following tags:
<ol type="i"> <li>First item</li> <li>Second item</li> <ul type="square"> <li>First item</li> <li>Second item</li> </ul> <li>Third item</li> </ol>