我正在使用 JTextPane
来显示字符和符号,其中后者由自定义绘制的 JComponents
表示。例如,文本 Pane 可能显示如下内容:
文本 Pane 是用户可编辑的,允许用户通过任何位置的按钮添加更多符号并替换所选文本。我通过 JTextPane.insertComponent()
方法执行此操作。在应用程序的某个时刻,我需要知道文本 Pane 中当前显示的内容,我指的不仅是输入的文本,还包括其中包含的确切组件。
我在使用 Positions
和 DocumentListeners
来管理我的文本 Pane 的内容时遇到了很多麻烦,但我总是造成比我解决的问题更多的问题。这就是为什么我最终决定,我的麻烦可能是由于我的设计错误造成的,所以我决定看看我是否无法通过文本 Pane 访问我的组件。
通过AbstractDocument
和其他相关类的文档和源代码搜索,我找到了接口(interface)javax.swing.text.Element
。然后我让我的应用程序输出
for(int i = 0; i < textPane.getDocument().getLength(); i++) {
System.out.println(((StyledDocument) textPane.getDocument()).getCharacterElement(i));
}
这给了我:
LeafElement(content) 0,4
LeafElement(content) 0,4
LeafElement(content) 0,4
LeafElement(content) 0,4
LeafElement(component) 4,5
LeafElement(content) 5,9
LeafElement(content) 5,9
LeafElement(content) 5,9
LeafElement(content) 5,9
LeafElement(component) 9,10
看到我得到的 LeafElements
似乎有某种关于在 Document
中的哪个位置显示什么的信息,我想这一定是可能的获取该位置的实际内容。在又搜索了半小时如何获取每个元素代表的内容后,我放弃了,决定在这里发布我的问题,希望你们中的一些人可能知道如何完成这个!?
我看过这个question有人试图通过 textPane.getComponents()
访问组件,它返回一个组件数组,其中组件的确切数量实际包含在 JTextPane
中,但它们都是javax.swing.text.ComponentView$Invalidator
类型的,这显然对我没有用。也许我只是不知道如何从这里正确地继续,因为转换为我的符号的原始类型不起作用。
tl;dr
如何获取位于 JTextPane
文本内的 JComponent
及其在文本 Pane 中的位置?
最佳答案
您可以遍历文本 Pane 的 StyledDocument
来查找表示组件或图标的元素,如下所示。
BranchElement(section) 0,7 BranchElement(paragraph) 0,7 LeafElement(content) 0,4 LeafElement(icon) 4,5 class javax.swing.plaf.IconUIResource LeafElement(component) 5,6 class javax.swing.JLabel LeafElement(content) 6,7
SSCCE:
/**
* @see http://stackoverflow.com/a/15669307/230513
* @see http://stackoverflow.com/questions/2883413
*/
public class DocumentParse {
private static final String ELEM = AbstractDocument.ElementNameAttribute;
private static final String ICON = StyleConstants.IconElementName;
private static final String COMP = StyleConstants.ComponentElementName;
public static void main(String args[]) throws Exception {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JTextPane jtp = new JTextPane();
StyledDocument doc = (StyledDocument) jtp.getDocument();
SimpleAttributeSet normal = new SimpleAttributeSet();
StyleConstants.setFontFamily(normal, "Serif");
StyleConstants.setFontSize(normal, 72);
StyleConstants.setForeground(normal, Color.blue);
doc.insertString(doc.getLength(), "Test", normal);
jtp.setSelectionStart(doc.getLength());
jtp.insertIcon(UIManager.getIcon("OptionPane.warningIcon"));
jtp.setSelectionStart(doc.getLength());
jtp.insertComponent(new JLabel("Label"));
jtp.setSelectionStart(doc.getLength());
ElementIterator iterator = new ElementIterator(doc);
Element element;
while ((element = iterator.next()) != null) {
System.out.println(element);
AttributeSet as = element.getAttributes();
if (as.containsAttribute(ELEM, ICON)) {
System.out.println(StyleConstants.getIcon(as).getClass());
}
if (as.containsAttribute(ELEM, COMP)) {
System.out.println(StyleConstants.getComponent(as).getClass());
}
}
f.add(jtp);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
关于java - 通过 javax.swing.text.Element 从 JTextPane 获取一个组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15661508/