I need to implement a TextPane to which contents will be added dynamically on the go. It can have text and links.
The links can be displayed by adding JLabel to the textPane. But the issue is, all the text the textPane has, needs to be stored to a file. textPane.getText() returns text but it does not append any text of the components added to the textPane.
For example if textPane has a String(“Hello”) and a JLabel(“label”). textPane.getText() returns only “hello”. How do I retrieve “label” ??
Normally
getText()represents content of the Document. For your goal you should iterate through the CharacterElementsand for each of them check whether attributes hasComponent. Then cast to e.g.JLabeland get text etc.Also check how the component is added to the
JTextPane. You can add your component into attributes and insert an appropriate string with the attributes.Then your
Documenthas the text of theJLabel.