views:

141

answers:

2

I have an HTML document being displayed by a JTextPane that works very nicely. I have some buttons that I interact with using ActionListeners for a ButtonModel, and I hook into state links (#foo) to handle simple internal app links.

My problem is that I have a INPUT element that I need to change the "value" of so the text changes in the box.

+1  A: 

JTextPane's getText()and setText() methods give you full access to the text displayed by the component. If that happens to be HTML, then the text you're dealing with is a HTML document and you need to change that HTML text just as you would if you were displaying HTML directly.

If you have an INPUT with type="text" or default type, then you display a text field, and its displayed value is controlled by the value= attribute.

To do that, you need to do some in-code text editing of the text value of your JTextPane. You can use IndexOf() to find the offset of your INPUT tag, then again to find the value or the closing angle bracket, and then you insert your desired value at the location you wanted.

When you have your new String all set up, put it back in the JTextPane using setText(). Done.

Carl Smotricz
+1  A: 

HTMLDocument has a getElement method for getting the javax.swing.text.Element with a given id attribute. Use this to get your input element, then call

htmlDocument.setOuterHtml(
    inputElement, 
    "<input id=\"foo\" value=\"" + escapeHtml(newValue) + "\">"
)

to replace the value of your inputElement with the new value.

Note, I haven't tried this. Let us know if it works!

Sam Barnum