DefaultStyledDocument
Java Swing class JTextPane
is used for the core operation of VTT.
MouseListener
and CaretListener
are added in JTextPane for mouse control and caret control.
JTextPane.getStyledDocument( )
is used for core operations of changing color, style, size, font, etc.. The sample code is implemented in
VttDocument.java
as follows:
// get doc DefaultStyledDocument doc = (DefaultStyledDocument) JTextPane.getStyledDocument( ); // set SimpleAttibuteSet by StyleConstants SimpleAttributeSet sas = new SimpleAttributeSet(); StyleConstants.setBold(sas, tag.IsBold()); StyleConstants.setItalic(sas, tag.IsItalic()); StyleConstants.setUnderline(sas, tag.IsUnderline()); StyleConstants.setForeground(sas, TEXT_COLOR); StyleConstants.setBackground(sas, TEXT_BG_COLOR); StyleConstants.setFontFamily(sas, tag.GetFontFamily()); StyleConstants.setFontSize(sas, tag.GetFontSize()); // init/load doc doc.remove(0, doc.getLength()); doc.insertString(doc.getLength(), tagDocument.GetText(), sas); // set style to doc doc.setCharacterAttributes(offset, length, sas, false);