从JTextPane通过javax.swing.text.Element获取组件?
我使用JTextPane
来显示字符和符号,后者由自定义绘制的JComponents
表示。 例如,文本窗格可能显示如下内容: 文本窗格是用户可编辑的,允许用户通过任何位置的按钮添加更多符号,并替换所选文本。 我是通过JTextPane.insertComponent()
方法完成的。 在应用程序的某个时刻,我需要知道文本窗格中当前显示的内容,并且我不仅指输入的文本,还指其中包含的确切组件。
我经历了与Positions
和DocumentListeners
广泛麻烦来管理我的文本窗格的内容,但是我一直造成比我解决的更多问题。 这就是为什么我最终决定,我的麻烦可能是由于我的设计错误,所以我决定看看,如果我无法通过文本窗格访问我的组件。
通过搜索AbstractDocument
和其他相关类的文档和源代码,我找到了接口javax.swing.text.Element
。 然后我让我的应用程序输出
for(int i = 0; i < textPane.getDocument().getLength(); i++) { System.out.println(((StyledDocument) textPane.getDocument()).getCharacterElement(i)); }
这给了我:
LeafElement(内容)0,4
LeafElement(内容)0,4
LeafElement(内容)0,4
LeafElement(内容)0,4
LeafElement(组件)4,5
LeafElement(内容)5,9
LeafElement(内容)5,9
LeafElement(内容)5,9
LeafElement(内容)5,9
LeafElement(组件)9,10
看到我所获得的LeafElements
似乎有关于在Document
中哪个位置显示的内容的某些信息,我认为必须能够获得该位置的实际内容。 在搜索了另外半小时后如何获得每个元素所代表的内容后,我放弃了并决定在这里发布我的问题,希望你们中的一些人可能知道如何实现这个目标!
我已经看到这个问题 ,有人试图通过textPane.getComponents()
访问组件,它返回一个组件数组,其中包含JTextPane
实际包含的组件的确切数量,但它们都是javax.swing.text.ComponentView$Invalidator
类型javax.swing.text.ComponentView$Invalidator
,这显然对我没用。 也许我只是不知道如何从这里正确地继续,因为对我的符号的原始类型的强制转换不起作用。
TL;博士
如何获取JTextPane
文本中的JComponent
及其在文本窗格中的位置?
您可以遍历文本窗格的StyledDocument
以查找表示组件或图标的元素,如下所示。
BranchElement(部分)0,7 BranchElement(段落)0,7 LeafElement(内容)0,4 LeafElement(icon)4,5 class javax.swing.plaf.IconUIResource LeafElement(组件)5,6 class javax.swing.JLabel LeafElement(内容)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); } }
从ComponentView可以看到,原始组件是javax.swing.text.ComponentView$Invalidator
的第一个(也是唯一的)子级。
您可以获取无效器列表并使用其子代来插入组件。