Get the Title of an HTMLDocument
R. Kevin ColeDecember 01, 2000

Have you ever wanted to display the title of an HTMLDocument in your swing application but didn't find a convenient getTitle() method? It isn't there because the title is stored as a property in the Document class. You'll have to use the getProperty() and putProperty() methods to access the "title property" of the Document.

Try doing the following:

String  title = (String)  doc.getProperty( Document.TitleProperty ); 

Here is an example that loads an HTML string into an HTMLDocument and accesses the title property.

Source code: HTMLDocTitle.java
import javax.swing.text.html.*;
import javax.swing.text.*;
import java.io.*;

public class HTMLDocTitle
{
public static void main(String argv[])
{
String test = "<!DOCTYPE HTML PUBLIC \"-//w3c//DTD html 3.2//EN\">"
+ "<html><head>"
+ "<title>This is the title of my HTML document</title>"
+ "</head>"
+ "<body>"
+ "This is some text in the body of my document"
+ "</body></html>";

HTMLEditorKit kit = new HTMLEditorKit();
try
{
HTMLDocument doc = new HTMLDocument();
kit.read(new StringReader(test), doc, 0);
String title = (String) doc.getProperty(Document.TitleProperty);
System.out.println("HTMLDocument Title: " + title);
} catch (Exception e)
{
System.err.println("Unexpected " + e + " thrown");
}
}
}

Which will print:
HTMLDocument Title: This is the title of my HTML document