如何在框架的左下角添加进度条

时间:2014-09-15 08:00:49

标签: java swing jtextarea jprogressbar

我想在加载文件时添加JProgressBar。我为它编写了代码。它正在工作。但是,之前我使用Jfferedxt的BufferedReader和setText()方法加载文件,在这种情况下需要6加载44MB textfile的时间。现在我使用FileInputStream读取数据ProgressMonitorInputStream来显示进度条。在这种情况下花了17秒的时间来加载44MB文件。如何解决这个问题我想向我添加进度条在左下角的框架。请检查它并给我建议。

我的工作代码:

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

1 个答案:

答案 0 :(得分:1)

  

执行tx.setText()方法只是为了加载文件。

不要使用setText()方法。

相反,您应该使用JTextArea API提供的read(...)方法,该方法从JTextCompnent继承该方法。

然后,您可以使用带有ProgressMonitorInputStream的InputStreamReader来读取文件。

编辑:

简单SSCCE读取文件:

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