-1

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;                  
}
user3912886
  • 53
  • 2
  • 9
  • Please do provide information, of what you been advised previously, and how you tried to accomplish that thingy. Atleast a small idea of your effort, will help us all, to know, where exactly you going wrong!!!! – nIcE cOw Sep 15 '14 at 08:02
  • In my program,I use textArea.setText(text),How can I know the amount of data is loaded.That's I want. – user3912886 Sep 15 '14 at 08:33
  • 1
    See [How to Use Progress Monitors](http://docs.oracle.com/javase/tutorial/uiswing/components/progress.html#monitors) & get back to us if you have any problems ..with the **code** that is having the problem. Voting to close as 'too broad'. – Andrew Thompson Sep 15 '14 at 08:45
  • Hi,I add my open code.Here the problem is upto toString() method no time taken to process.The execution of tx.setText() method is only taking to load the file.How can show the Status bar when loaded the large file for this program.Please check it. – user3912886 Sep 15 '14 at 09:51
  • Please give me some suggestions. – user3912886 Sep 15 '14 at 12:22
  • In JTextArea read() method also taking single step to read the file .how can provide logic for JProgressBar. – user3912886 Sep 16 '14 at 07:27

1 Answers1

1

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();
            }
        });
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
  • I edit my code with read() method.In JTextArea read() method also taking single step to read the file .how can provide logic for JProgressBar.Please provide solution for this. – user3912886 Sep 16 '14 at 07:27
  • Please check second problem also. – user3912886 Sep 16 '14 at 10:20
  • @user3912886 read the `ProgressMonitorInputStream` API for an example. Since the read(...) method needs a `Reader`, you will need to use an `InputStreamReader`. – camickr Sep 16 '14 at 20:15
  • Please provide code for this.I am new for swing concepts. – user3912886 Sep 17 '14 at 05:14
  • I want to add progress bar at the bottom left of the frame when loaded a large file.Please give me some suggestions,How to do this.Please give me some brief explanation, If possible provide some useful code related to this.Thank you in advance. – user3912886 Sep 18 '14 at 12:31
  • I did give you suggestion. I suggested you read the API which has some example code. You have also been given a link to the Swing tutorial on `How to Use Progress Monitors` which has explanation and working examples. I suggest you start with a working example and then customize the code to use the `ProgressMonitorInputStream`. You can also search the web for examples that use this class. Finally, you can post a proper [SSCCE](http://sscce.org/) that demonstrates your problem. These are all steps that you take to learn problem solving skills. I have not used this class before. – camickr Sep 18 '14 at 14:31
  • I had modified my post.Earlier I used BufferedReader for read the data 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 and it shows when loaded large text files only.Please check it above code and give me some suggestions. – user3912886 Sep 19 '14 at 11:23
  • @user3912886, I suggested you use the `read(...)` method of the text area. Once you use the `InputStreamReader` you should also be able to wrap that in a BufferedReader the way you did before which should improve performance. – camickr Sep 19 '14 at 14:42
  • @user3912886, `Can you modify my...code`, No because you haven't posted a `SSCCE` as has been suggested. However, I will post a `SSCCE` that shows how to use the read() method of a JTextArea. I will let you figure out how to use the `ProgressMonitorInputStream`. As I already mentioned I have never used this class and don't have a large file to test its functionality, so I will leave that up to you. Again you can always search the forum/web for examples. Don't expect people to write the code for you! – camickr Sep 21 '14 at 01:31