I am using non-editable JTextPane to show some data in HTML format. I have set contentType to “text/html” and it works. Now I wanted to add HTML checkboxes in JTextPane, and listen to their change, and be able to retrieve if a specific checkBox is selected. Is this possible?
The text of JTextPane is in this format:
<html><form>
<input type="checkbox" name="checkbox1" value="value" /> checkbox1<br />
</form></html>
Should I be using JTextPane for this purpose at all, or is there a better control? Regular check boxes are not an option, because I need a HTML format for styling it easily.
Generally you would use a JEditorPane to display HTML.
Depending on your requirement there are two ways to go about this:
Swing components are actually added to the editor pane. So once the docuemnt has been parsed and the editor pane has been revalidated() you should be able to just get a list of all the components added to the editor pane. You can check the class name to find the components you want.
The HTMLDocument contains attributes about each component added including the components model. So you can search the document to get the model for every checkbox.
Here is some general code to get your started: