I want to add JProgressBar while loading a file.I wrote code for that.It's working.But,Earlier I used BufferedReader and setText() method of JTextArea for load a file,In this case it took 6 seconds time to load 44MB textfile.Now I use FileInputStream for read the data ProgressMonitorInputStream for show the progress bar.In this case took 17 seconds time to load the 44MB file.How can I solve this issue and I want to add Progress bar to my frame at the left bottom corner.Please check it and give me suggestions.
My working code:
public class ProgressbarAction extends javax.swing.JFrame implements Runnable {
JTextArea textArea;
int i=0;
JScrollPane scrollPane;
JTextField statusBar;
public ProgressbarAction() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
tabbedPane = new javax.swing.JTabbedPane();
fileMenubar = new javax.swing.JMenuBar();
fileMenu = new javax.swing.JMenu();
open = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
fileMenu.setText("File");
open.setText("Open");
open.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
openActionPerformed(evt);
}
});
fileMenu.add(open);
fileMenubar.add(fileMenu);
setJMenuBar(fileMenubar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
);
pack();
}// </editor-fold>
private void updateStatus(int linenumber, int columnnumber) {
statusBar.setText("Line: " + linenumber +" "+ " Column: " + columnnumber);
}
Thread athread;
private void openActionPerformed(java.awt.event.ActionEvent evt) {
athread = new Thread (this);
athread.start ();
}
public void run(){
FileDialog fd = new FileDialog(ProgressbarAction.this, "Select File", FileDialog.LOAD);
fd.setVisible(true);
String title;
if (fd.getFile() != null) {
title=fd.getFile();
File file=new File(fd.getDirectory() + fd.getFile());
final JInternalFrame internalFrame = new JInternalFrame("",true,true);
textArea = new JTextArea();
textArea.setFont(new java.awt.Font("Miriam Fixed", 0, 13));
i+=1;
internalFrame.setName("Doc "+i);
scrollPane=new JScrollPane(textArea);
scrollPane.setPreferredSize (new Dimension(350,350));
internalFrame.add(scrollPane,BorderLayout. CENTER);
internalFrame.setTitle(title);
tabbedPane.add(internalFrame);
internalFrame.setVisible(true);
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream (file);
} catch (FileNotFoundException ex) {
Logger.getLogger(ProgressbarAction.class.getName()).log(Level.SEVERE, null, ex);
}
ProgressMonitorInputStream pmInputStream = new
ProgressMonitorInputStream (this," get file ... ", inputStream);
ProgressMonitor pMonitor =
pmInputStream.getProgressMonitor ();
pMonitor.setMillisToDecideToPopup (0);
final Scanner in = new Scanner(pmInputStream);
textArea.setText("");
while (in.hasNextLine()) {
String line = in.nextLine();
textArea.append(line+"\n");
}
if(pMonitor.isCanceled()) {
textArea.append ("\n \n read files interrupt");
}
in.close();
textArea.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(ProgressbarAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ProgressbarAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ProgressbarAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ProgressbarAction.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ProgressbarAction().setVisible(true);
}
});
}
private javax.swing.JMenu fileMenu;
private javax.swing.JMenuBar fileMenubar;
private javax.swing.JMenuItem open;
private javax.swing.JTabbedPane tabbedPane;
}
The execution of tx.setText() method is only taking to load the file.
Don't use the setText() method.
Instead you should be using the read(...)
method provided by the JTextArea API which inherits the method from JTextCompnent.
Then you can use an InputStreamReader with a ProgressMonitorInputStream to read the file.
Edit:
Simple SSCCE to read a file:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;
class TextAreaLoad
{
private static void createAndShowGUI()
{
final JTextArea edit = new JTextArea(30, 60);
JButton read = new JButton("Read TextAreaLoad.txt");
read.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
FileInputStream fis = new FileInputStream( "TextAreaLoad.txt" );
InputStreamReader reader = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(reader);
edit.read( br, null );
br.close();
edit.requestFocus();
}
catch(Exception e2) { System.out.println(e2); }
}
});
JFrame frame = new JFrame("TextArea Load");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new JScrollPane(edit), BorderLayout.NORTH );
frame.add(read, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo( null );
frame.setVisible(true);
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}