2

I want to load a large file(1MB of plain text) contents using JTextPane. It took nearly two minutes to load a large file. I want to load the large file into JTextPane within seconds. If it possible to improve the performance of the JTextPane. My Open action code is available in openActionPerformed() method. Please check it and give me some suggestions. Thank you.

Constructor code:

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

OpenActionPerformed() method:

    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);        
   }                   

Main method:

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;                
}
  • *"I want to load a large file(1MB).."* That's a whole heapin' helpin' you're dumping on the hapless user. *"..contents"* What format is this content (plain text, HTML, RTF..)? – Andrew Thompson Aug 28 '14 at 06:00
  • 2
    http://java-sl.com/JEditorPanePerformance.html – StanislavL Aug 28 '14 at 06:06
  • Ha,I had tried that techniques.In my program I used setPage() method.That is in the above provided link.But,this is also taking much time to load. – user3792689 Aug 28 '14 at 06:17
  • 2
    @StanislavL maybe you have look at very similair [question about by trashgod](http://stackoverflow.com/questions/25526833/loading-and-displaying-large-text-files) – mKorbel Aug 28 '14 at 06:17
  • @StanislavL,@mKorbel :I tried your provided links.But,those are also not improve the performance.Please give me an alternate solution for the above. – user3792689 Aug 28 '14 at 07:52
  • There is no simple common solution for the performance issue. It depends on the EditorKit use use, Document structure/attributes, views, content e.g. LTR/RTL languages etc. – StanislavL Aug 28 '14 at 08:31
  • @mKorbel about the trashgod's question. it's the same I would write a custom view (just one as replacement of the default view. I would use monospaced font to measure lines faster. – StanislavL Aug 28 '14 at 08:32
  • @StanislavL thanks for detailed answer there +1 – mKorbel Aug 28 '14 at 09:03
  • @StanislavL :Can you make modifications to my code by write a custom view of the default view.I don't how to write the custom view. – user3792689 Aug 28 '14 at 09:10
  • 4
    "How to improve the performance of JTextPane when loading large files": Don't load large files. It's a user interface, not a database. – user207421 Aug 28 '14 at 09:55
  • @EJP How can I solve this issue, – user3792689 Aug 28 '14 at 09:58
  • Anybody provide the solution for this.I was tried this one for past 15 days.But, I couldn't done.Please try this one. – user3792689 Aug 28 '14 at 12:19

1 Answers1

1

For a 1 MB text file it's impossible to take two minutes to load unless it's read from a diskette or alike.

Putting it all into a user interface is a non-sense, nobody can do anything with it. Scrolling using a scroll bar get completely unusable, too. Allow the user to enter a starting offset (in lines), read the file using Files.readLines into a List<String>, and display a few lines only.

Code idea

All non-JDK classes come from 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));
Community
  • 1
  • 1
maaartinus
  • 40,991
  • 25
  • 130
  • 292
  • If possible,Please provide an working code that.I didn't have much experience on Swing components. – user3792689 Aug 30 '14 at 03:06
  • Please modify my code as you suggested.This is very tough task for me. – user3792689 Sep 01 '14 at 06:20
  • @user3792689 Sorry, no time to do it all. Added a short example code. – maaartinus Sep 01 '14 at 13:03
  • Thank you.But,The above code shows some errors in the following areas...Chatsets.UTF8,myOffsetTextField and Joiner.Please correct it.Please spend few minutes time for me.This is my sincere request. – user3792689 Sep 02 '14 at 05:44
  • It shows an Exception package com.google.common.base does not exist of Charsets and Joiner classes.I replace myOffsetTextField with textPane variable.Please check it. – user3792689 Sep 02 '14 at 06:05
  • I resolve the problems of CharSets and Joiner classes.But,only one error is raised.Here what refers the myOffsetTextField variable. – user3792689 Sep 02 '14 at 06:57
  • @user3792689 `myOffsetTextField` is an additional text field. Above, I wrote *Allow the user to enter a starting offset (in lines)*, that's it. – maaartinus Sep 02 '14 at 14:17
  • I edit my openActionPerformed() code Through the above suggestions.But,It throws an exception called "java.nio.charset.MalformedInputException: Input length = 1".Please check it. – user3792689 Sep 03 '14 at 07:06
  • @user3792689 My code is assuming that the input is `UTF-8` encoded (the only sane encoding). But it is not. So substitute proper `Charset` argument. – maaartinus Sep 03 '14 at 17:59
  • Still It throws an exception.I had changed Charsets.UTF_8 to Charset.defaultCharset() or Encoding.Still it raises the same Exception when loading large data files.check it once. – user3792689 Sep 04 '14 at 08:23
  • @user3792689 So find out the encoding of your file, asking me doesn't help as I don't have your file. Or use ISO_8859_1, which AFAIK accepts everything. Or use a different method for reading the file and a `CharsetDecoder` configured to ignore errors. Or fix your file. – maaartinus Sep 04 '14 at 16:55
  • Ha My file is available in OpenActionPerformed() method as above.Check it once.Finally,I want to enhance the speed up the JTextPane when loading the large data file.It's my main thing.Please do it.Thank you. – user3792689 Sep 06 '14 at 06:32