2013-06-05 47 views
1

我正在製作一個GUI應用程序,點擊按鈕後,我們讀取一個二進制文件並在TextArea中顯示數據(數據爲327680字節)。Swing TextArea函數setLineWrap使執行變慢

現在,如果我使用textArea.setLine(true),那麼它會減慢執行速度,執行和dislpays值大約需要1分鐘。

但是,如果我不使用textArea.setLine(true)那麼代碼將在不到1秒內執行。

UPDATED CODE。

public class App{ 

private static final String INPUT_FILE = "E:\\arun\\files\\Capture_Mod1.BIN"; 
private static final String OUTPUT_FILE = "E:\\arun\\files\\ISO_Write.BIN"; 
private static final String IMAGE_FILE = "E:\\arun\\files\\Image.jpg"; 

private JFrame frame; 
private JTextArea textArea; 
private JButton btnNewButton; 
private ISOImageDataWorker worker; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       App window = new App(); 
       window.frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 


} 

/** 
* Create the application. 
*/ 
public App() { 
    initialize(); 
} 

/** 
* Initialize the contents of the frame. 
*/ 
private void initialize() { 
    frame = new JFrame(); 
    frame.setBounds(100, 100, 450, 300); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    GridBagLayout gridBagLayout = new GridBagLayout(); 
    gridBagLayout.columnWidths = new int[]{0, 0}; 
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0}; 
    gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE}; 
    gridBagLayout.rowWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE}; 
    frame.getContentPane().setLayout(gridBagLayout); 

    textArea = new JTextArea(5, 20); // **HERE IS THE PROBLEM** 
    textArea.setLineWrap(true); 
    textArea.setWrapStyleWord(true); 
    JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 
    GridBagConstraints gbc_textArea = new GridBagConstraints(); 
    gbc_textArea.insets = new Insets(0, 0, 5, 0); 
    gbc_textArea.gridx = 0; 
    gbc_textArea.gridy = 1; 
    frame.getContentPane().add(scrollPane, gbc_textArea); 

    btnNewButton = new JButton("ISO 19794 Data"); 
    GridBagConstraints gbc_btnNewButton = new GridBagConstraints(); 
    gbc_btnNewButton.gridx = 0; 
    gbc_btnNewButton.gridy = 2; 
    frame.getContentPane().add(btnNewButton, gbc_btnNewButton); 

    btnNewButton.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) { 
      worker = new ISOImageDataWorker(); 
      worker.execute(); 
     } 
    }); 

} 

private class ISOImageDataWorker extends SwingWorker<String , Object>{ 

    @Override 
    protected String doInBackground() throws Exception { 
     String str = processData(); 
     return str; 
    } 

    @Override 
    protected void done(){ 
     try { 
      System.out.println("cm1"); 
      textArea.setText(get()); 
      System.out.println("cm2"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

public String processData(){ 
    DataReadWrite data = new DataReadWrite(); 

    //Read data 
    byte[] imageData = data.readData(INPUT_FILE); 
    System.out.println("Image data length is : "+ imageData.length); 

    // Generate Finger Image Data General Header 
    FingerImageDataGeneralHeader generateHeader = new FingerImageDataGeneralHeader(); 
    byte[] resultData = generateHeader.createGeneralHeader(imageData); 

    // Write data to binary file 
    data.writeData(resultData, OUTPUT_FILE); 

    // Create Image from binary data 
    CreateTiff createTiff = new CreateTiff(); 
    createTiff.create(resultData, IMAGE_FILE); 

    String str = bytesToHex(resultData); 

    return str; 
} 

public static String bytesToHex(byte[] bytes) { 
    StringBuilder sb = new StringBuilder(); 
    for (byte b : bytes) { 
     sb.append(String.format("%02X ", b)); 
    } 
    return sb.toString(); 
} 

}

我不知道我在幹什麼錯誤。請幫忙。

+0

二進制數據通常沒有任何空格,所以沒有什麼可以打包的。不知道它是否會有所作爲,但不是一次更新整個文件的textarea,而是嘗試使用append()方法並更新字符塊中的文本區域,一次可能是128(?)。 – camickr

回答

3
btnNewButton.addActionListener(new ActionListener() { 
    public void actionPerformed(ActionEvent arg0) { 
    >> processData(); << 
     textArea.setText(str); 
    } 
}); 

不要在GUI線程(EDT)上執行耗時的代碼。使用SwingWorker

+0

感謝您的回覆。我已經完成了你的建議,值得爲未來做好準備。現在我正在使用SwingWorker。但是,當我用textArea.setLineWrap(true)運行代碼時,執行需要大約2分鐘,如果將其設置爲false,則只需要1秒。爲什麼這樣? –