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