2014-05-30 248 views
2

我有一個很長的字符串,我從書中得到。我使用setText()方法將其顯示在JTextArea中。它凍結了用戶界面並花費了很多時間。我該如何解決這個問題?JTextArea setText(veryLongString)花費的時間太長

這裏是SSCCE:

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 

@SuppressWarnings("serial") 
public class SSCCE extends JFrame { 

    JTextArea textArea; 

    public SSCCE() { 
     String text = buildLongString(400000); 
     textArea = new JTextArea(); 
     textArea.setText(text); 
     textArea.setLineWrap(true); 
     add(new JScrollPane(textArea)); 

     setSize(400, 350); 
     setLocationRelativeTo(null); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     setVisible(true); 
    } 

    private String buildLongString(int length) { 
     StringBuilder builder = new StringBuilder(); 
     for (int i = 0; i < length; i++) { 
      builder.append("x"); 
     } 
     return builder.toString(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new SSCCE(); 
      } 
     }); 
    } 

} 
+2

您真的將400,000個字符的文字轉儲到不幸的用戶身上?這一切的重點是什麼? –

+0

我看不到任何需要的代碼修改。 – Arijit

+3

有趣的是,它是'textArea.setLineWrap(true);'(間接不在通話中),它需要所有的時間。把文本放在那裏實際上很快。例如,連接「x」,如果連接「x \ n」,它變得非常快。 – DSquare

回答

5

從構建GUI一個單獨的線程創建DefaultStyledDocument似乎創造了巨大的文本區的最快方法。 DefaultStyledDocument是線程安全的。

下面是我用來測試DefaultStyledDocument的代碼。我用空格創建了文本,以便Swing代碼行換行有機會工作。

package com.ggl.testing; 

import java.util.Random; 

import javax.swing.JFrame; 
import javax.swing.JScrollPane; 
import javax.swing.JTextArea; 
import javax.swing.SwingUtilities; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.DefaultStyledDocument; 

public class HugeTextArea implements Runnable { 

    private DefaultStyledDocument document; 

    private JFrame     frame; 

    private JTextArea    textArea; 

    public HugeTextArea() { 
     this.document = new DefaultStyledDocument(); 
     Runnable runnable = new Runnable() { 
      @Override 
      public void run() { 
       buildLongString(400000); 
      } 
     }; 
     new Thread(runnable).start(); 
    } 

    @Override 
    public void run() { 
     frame = new JFrame(); 
     frame.setTitle("Huge Text"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     textArea = new JTextArea(document); 
     textArea.setLineWrap(true); 
     frame.add(new JScrollPane(textArea)); 

     frame.setSize(400, 350); 
     frame.setLocationRelativeTo(null); 

     frame.setVisible(true); 
    } 

    private void buildLongString(int length) { 
     Random random = new Random(); 
     String[] chars = { "s", "t", "a", "y", " " }; 
     for (int i = 0; i < length; i++) { 
      try { 
       document.insertString(document.getLength(), 
         chars[random.nextInt(chars.length)], 
         null); 
      } catch (BadLocationException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new HugeTextArea()); 
    } 

} 
+0

運行方法是如何調用的? – usama8800

+0

@ usama8800:哪種運行方式? –

+0

第二種運行方法。 – usama8800