tadpole
tadpole

Reputation: 1219

how to get access to widget inside JEditorPane?

I am using a JEditorPane to display html text. If the html file contains

<form action="WORK3.html" method="get">
  <input type="text" value="Enter your name here!" size="25">
</form>

The JEditorPane will put a text field widget inside the editor widget. What I would like to do is retrieve the value inside the text field. How do I retrieve either the text value or the widget of the text field ?

Upvotes: 0

Views: 518

Answers (3)

tadpole
tadpole

Reputation: 1219

Thanks Stanislav for taking the time to answer. Trying to retrieve the component was too hard for me. I found a way around the problem. I hope this will help someone else. Although I could not find a way to get access to the widget, I can create my own text widget and replace the default one. That way I have control over widget. To do this you have to subclass the HTMLEditorKit.HTMLFactory. In this class, you need to override the

public View create (Element elem_) {
    Document doc = elem_.getDocument();
    Object obj = elem_.getAttributes().
        getAttribute(StyleConstants.NameAttribute);

    HTML.Tag tag = (HTML.Tag) obj;

    if (tag.toString().equals ("input")) {
        // you can replace your widget here if you want
        // i choose to use the default but save the view for
        // my own use later
        ComponentView view = (ComponentView)super.create (elem_);
        // save the component view to where you want to access 
        // later.  you can retrieve the component from the 
        // ComponentView and cast it to back to JTextField
        _editor.saveView (view);

        return (view);
    }

    else {
        return (super.create (elem_);
    }
}

You have to do this at least once for each type of widget that you want to control. It is a pain, but it does the job.

Upvotes: 0

marcelof01
marcelof01

Reputation: 1

When you inserted a component inside a textPane you just have to "unwrap" it from its Invalidator class.

//Loop through the components, maybe looking for some in particular

for (Component cont : myTextPane.getComponents()) {
//Unwrap your component, means take the only one component from the wrapper container
    Component comp = ((Container) cont).getComponent(0);

    //Do your things with your component
    // ...      
}

Upvotes: 0

StanislavL
StanislavL

Reputation: 57421

HTML controls are added to the JEditorPane wrapped in private inner class Invalidator extends Container.

So you can get all children components of the JEditorPane. Each of them should be Invalidator instance. The only child of the Invalidator is the edit component itself (e.g. JTextFlied for <input> tag).

Upvotes: 1

Related Questions