I am working on a registrar to show the result as part of the larger Java swing GUI. Unfortunately, after adding, I experienced a slowdown. I tracked the issue for repeated calls Document.insertString().
I did a test that shows this slowdown:
LogPanel.java
public class LogPanel extends JPanel{
private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private JEditorPane textPane;
private static int numTextRows = 50;
private SimpleAttributeSet keyWord;
private Document document;
public LogPanel() {
super(new BorderLayout());
add(makePanel(), BorderLayout.CENTER);
}
private Component makePanel() {
textPane = new JTextPane();
document = textPane.getDocument();
keyWord = new SimpleAttributeSet();
StyleConstants.setForeground(keyWord, Color.BLACK);
textPane.setEditable(false);
textPane.setFont(new Font("monospaced", Font.PLAIN, 12));
DefaultCaret caret = (DefaultCaret) textPane.getCaret();
caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE);
JPanel textPaneWrapper = new JPanel();
textPaneWrapper.setLayout(new BorderLayout());
textPaneWrapper.add(textPane);
JScrollPane areaScrollPane = new JScrollPane(textPaneWrapper);
areaScrollPane.getVerticalScrollBar().setUnitIncrement(20);
areaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
areaScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
areaScrollPane.setPreferredSize(new Dimension(250, 250));
StyleConstants.setBackground(keyWord, areaScrollPane.getBackground());
textPane.setBackground(areaScrollPane.getBackground());
return areaScrollPane;
}
public void appendResult(final String action, final String result, final Color color) {
Date now = new Date();
String strDate = df.format(now);
String paddedAction = String.format("%-19s", action);
StyleConstants.setForeground(keyWord, color);
try {
document.insertString(document.getLength(), strDate + " " + paddedAction + " " + result + "\n", keyWord);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
if(!textPane.hasFocus()) {
textPane.setCaretPosition(document.getLength());
}
}
public void appendResult(String action, String result) {
appendResult(action, result, Color.BLACK);
}
}
LogTester.Java
public class LogTester extends JFrame{
private LogPanel logPanel;
private JButton pushMe;
public LogTester(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
logPanel = new LogPanel();
this.add(logPanel);
pushMe= new JButton("Press Me");
LogTester self=this;
pushMe.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
self.addLotsOfStuff();
}
});
this.add(pushMe);
this.pack();
this.setVisible(true);
}
public void addLotsOfStuff(){
for(int i=0; i<1000; i+=1){
long start=System.currentTimeMillis();
for(int j=0; j<1000; j+=1){
String str="This is a very long peice of text designed to test the capabilites of our log panel. Move along, nothing to see here.";
logPanel.appendResult("HERP",str);
}
long end=System.currentTimeMillis();
System.out.println(end-start);
}
}
public static void main(String args[]){
LogTester test=new LogTester();
}
}
The program above tries to write a large number of lines to JTextPanewhere it is used Document.insertString(). The results of this program relate to:

- : . , , ( )
Java GUI - insertString? , . , .
JTextPane. . , . , .
, ?
JTextPane Document insertString()?