Lucas
Lucas

Reputation: 1270

Is it bad form to build a new DefaultStyledDocument every time I wish to clear one and start anew?

My program ends up taking a StyledDocumentobject from one JTextPane(A) and passing it off to another JTextPane(B). When I have finished passing, I wish for the JTextPane(A) to be clear of text and any formatting and basically be a fresh build of the object with its default JTextPane settings. To do this, I am currently doing something like:

//make things
JTextPane inputField = new JTextPane();
JTextPane outputField = new JTextPane();

//move inputField text (with formatting) from inputField to outputField
StyledDocument doc = inputField.getStyledDocument();
EditorKit kit = inputField.getEditorKit();
outputField.setStyledDocument(doc);
outputField.setEditorKit(kit);
outputField.revalidate();

//reset the inputField so that it's fresh and ready for new input
inputField.setStyledDocument(new DefaultStyledDocument());
inputField.setEditorKit(new StyledEditorKit());

I realize in this example I don't have any text or formatting being moved(just a blank document object), but those are the operations I am performing, and am curious if "newing" a kit and document are a lazy way to reset my JTextPane to default settings. Thanks in advance!

Upvotes: 1

Views: 288

Answers (1)

StanislavL
StanislavL

Reputation: 57421

IMHO it's absolutely fine to create a new instance of document. In fact it's faster because listeners don't update views to reflect empty Document and then the new Document's content.

BTW: no need to reset kit if it's the same class. It's enough to call setDocument()

Upvotes: 1

Related Questions