如何在加载大文件时提高JTextPane的性能

时间:2014-08-28 05:48:49

标签: java performance swing jtextpane

我想使用JTextPane加载一个大文件(1MB纯文本)内容。加载大文件花了将近两分钟。我想在几秒钟内将大文件加载到JTextPane中。如果可以提高JTextPane的性能。我的Open动作代码在openActionPerformed()方法中可用。请检查一下,并给我一些建议。谢谢。

构造函数代码:

public class OpenDemo extends javax.swing.JFrame {
JTextPane textPane;
JScrollPane scrollPane;
int i=0;
public OpenDemo() {
    initComponents();
    textPane=new JTextPane();
}

OpenActionPerformed()方法:

    private void openActionPerformed(java.awt.event.ActionEvent evt) {                                      
    int offset = 0;
    FileDialog fd = new FileDialog(OpenDemo.this, "Select File", FileDialog.LOAD);
    fd.setVisible(true);
    String title;
    String path;
    Path filePath = null;
    File file;
    if (fd.getFile() != null) {
       path = fd.getDirectory() + fd.getFile();
       file=new File(path);
       filePath=file.toPath();
       title=fd.getFile();
       JInternalFrame internalFrame = new JInternalFrame("",true,true); 
       i++;
       internalFrame.setName("Doc "+i);
       internalFrame.setTitle(title);
       scrollPane=new JScrollPane(textPane);
       internalFrame.add(scrollPane);
       tp.add(internalFrame);

       myOffsetTextField=new JTextField();
       List<String> allLines = null;
        try {
            allLines = Files.readAllLines(filePath, Charsets.UTF_8);
        }
        catch (MalformedURLException ex) {
         Logger.getLogger(OpenDemo.class.getName()).log(Level.SEVERE, null, ex);
        } 
        catch (IOException ex) {
         Logger.getLogger(OpenDemo.class.getName()).log(Level.SEVERE, null, ex);
        }

        try{
            offset = Integer.parseInt(myOffsetTextField.getText()); 
        }
        catch(NumberFormatException ne){      
        }
       int numberOfLinesToShow = 10000;
       int start = Math.min(allLines.size(), offset);
       int end = Math.min(allLines.size(), start + numberOfLinesToShow);
       List<String> sublist = allLines.subList(start, end);
       textPane.setText(Joiner.on('\n').join(sublist));
       textPane.setCaretPosition(0);        
   }                   

主要方法:

public static void main(String args[]) {
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(OpenDemo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new OpenDemo().setVisible(true);
        }
    });
}                   
private javax.swing.JMenu jMenu1;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem open;
private javax.swing.JTabbedPane tp;                
}

1 个答案:

答案 0 :(得分:1)

对于1 MB的文本文件,除非从磁盘或类似文件中读取,否则无法加载两分钟。

将它全部放入用户界面是没有意义的,任何人都无法做任何事情。使用滚动条滚动也完全无法使用。允许用户输入起始偏移量(以行为单位),使用Files.readLines将文件读入List<String>,并仅显示几行。

代码理念

所有非JDK类都来自Guava。

List<String> allLines = Files.readLines(file, Chatsets.UTF8);
int offset = Integer.parseInt(myOffsetTextField.getText());
int numberOfLinesToShow = 10000;
int start = Math.min(allLines.size(), offset);
int end = Math.min(allLines.size(), start + numberOfLinesToShow);

// a sane-sized list of at most `numberOfLinesToShow` lines
List<String> sublist = allLines.sublist(start, end);

textPane.setText(Joiner.on('\n').join(sublist));