我正在使用 JEditorPane 来显示 html 文本。如果 html 文件包含
<form action="WORK3.html" method="get">
<input type="text" value="Enter your name here!" size="25">
</form>
JEditorPane 将在编辑器小部件中放置一个文本字段小部件。我想做的是检索文本字段中的值。如何检索文本字段的文本值或小部件?
我正在使用 JEditorPane 来显示 html 文本。如果 html 文件包含
<form action="WORK3.html" method="get">
<input type="text" value="Enter your name here!" size="25">
</form>
JEditorPane 将在编辑器小部件中放置一个文本字段小部件。我想做的是检索文本字段中的值。如何检索文本字段的文本值或小部件?
HTML 控件被添加到封装在私有内部类 Invalidator extends Container 中的 JEditorPane 中。
因此,您可以获得 JEditorPane 的所有子组件。它们中的每一个都应该是 Invalidator 实例。Invalidator 的唯一子项是编辑组件本身(例如,JTextFlied 用于<input>
标记)。
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.
当您在 textPane 中插入组件时,您只需将其从其 Invalidator 类中“解包”即可。
//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
// ...
}