Apl*_*nus 5 java swing element jtextpane jcomponent
我使用a 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));
}
Run Code Online (Sandbox Code Playgroud)
这给了我:
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,这显然对我没用.也许我只是不知道如何从这里正确地继续,因为对我的符号的原始类型的强制转换不起作用.
TL;博士
如何从文本窗格中获取a JComponent,它位于文本内部JTextPane及其位置?
tra*_*god 10
您可以遍历文本窗格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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2198 次 |
| 最近记录: |