Hopeless Programmer
Hopeless Programmer

Reputation: 267

Reading Java class documentation from code into a GUI

My question today is I have a GUI that interfaces another program. I have a text area within my GUI which I would like to display the class documentation of specific components that the user selects.

Example: User selects the 'Protocol' setting I want it to show the JavaDoc for that 'Protocol'.

How do I go about recognising the correct Javadoc and presenting it within the text area?

Upvotes: 1

Views: 1332

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168835

..Javadoc.. presenting it within the text area?

Don't use a JTextArea, but a JEditorPane. The latter will render HTML.

Upvotes: 2

Stewart
Stewart

Reputation: 18303

I would suggest 3 possible approaches:

  • As part of your build, create Javadoc HTML as normal, and then include it in your deployable, and have your GUI use an embedded browser of some kind (eg. WebKit)
  • Study how it's been done by other GUI's, such as from Eclipse's Javadoc View, or using a project which extends Eclipse Javadoc View
  • Use a Java source (Javadoc) parsing library / tool, such as QDox

Example code from QDox:

JavaDocBuilder builder = new JavaDocBuilder();
builder.addSource(new FileReader("MyFile.java"));

Upvotes: 2

Holger
Holger

Reputation: 298459

The JavaDoc is not part of a Java program. So you have to bundle the resources with your program regardless of what approach you use.

So you have two possibilities:

  1. Generate the documentation with javadoc and bundle the result. It is easy to see how to map from a class name to the html page by looking at the generated files. The JavaDoc documentation (the Default doclet’s documentation to be more precise) contains more detailed descriptions

  2. If you want to generate the documentation on-the-fly you have to bundle the source code with your application. Further, your application have to run inside a JDK rather than a plain JRE to get hands on JavaDoc.

See http://docs.oracle.com/javase/7/docs/technotes/guides/javadoc/standard-doclet.html

Running the Standard Doclet Programmatically

The Javadoc tool has a programmatic interface with public methods for invoking the Javadoc tool from another program written in the Java language. These methods are located in class com.sun.tools.javadoc.Main in lib/tools.jar. …

public static int execute(String programName, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter, String defaultDocletClassName, String[] args)

This execute method is overloaded with variants taking fewer parameters.

Upvotes: 1

Related Questions