上次分别讲了Swing中进度指示器的两个类JProgressBar和ProgressMonitor
这次给各位介绍一下最常用也是最简单的ProgressMonitorInputStream
他即是一个包装流也是一个带有进多条的对话框,例如我们想从一个文件中
多出所有的内容可是这个文件特别大(同样适用于从网络上读取内容),在
读取文件的同时我们弹出一个对话框,显示一个进度条
主要的步骤很少,如下,
FileInputStream in = new FileInputStream(f);
ProgressMonitorInputStream progressin = new ProgressMonitorInputStream(parent,caption,in);
InputStreamReader reader = new InputStreamReader(progressin);
- import java.awt.EventQueue;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.ProgressMonitorInputStream;
- import javax.swing.filechooser.FileFilter;
- public class TextFrame extends JFrame implements ActionListener {
- public static final int DEFAULT_WIDTH = 350;
- public static final int DEFAULT_HEIGHT = 450;
- private JMenuBar menuBar;
- private JMenu fileMenu;
- private JMenuItem openItem;
- private JFileChooser chooser;
- private JMenuItem exitItem;
- private JTextArea textArea;
- private ProgressMonitorInputStream progressIn;
- public TextFrame() {
- this.menuBar = new JMenuBar();
- this.fileMenu = new JMenu("File");
- this.openItem = new JMenuItem("open");
- this.exitItem = new JMenuItem("exit");
- this.chooser = new JFileChooser();
- textArea = new JTextArea();
- init();
- eventHandle();
- }
- private void init() {
- // add menu
- this.menuBar.add(this.fileMenu);
- this.fileMenu.add(this.openItem);
- this.fileMenu.add(this.exitItem);
- this.setJMenuBar(this.menuBar);
- }
- private void eventHandle() {
- this.openItem.addActionListener(this);
- this.exitItem.addActionListener(this);
- }
- public void showMe() {
- this.setTitle("Bounce");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setSize(DEFAULT_HEIGHT, DEFAULT_WIDTH);
- this.setVisible(true);
- }
- public void actionPerformed(ActionEvent e) {
- Object source = e.getSource();
- if (source.equals(this.openItem)) {
- this.chooser.setCurrentDirectory(new File("."));
- chooser.setFileFilter(new FileFilter() {
- public boolean accept(File f) {
- String filename = f.getName().toLowerCase();
- return filename.endsWith(".txt") || f.isDirectory();
- }
- public String getDescription() {
- return "Text File";
- }
- });
- int r = chooser.showOpenDialog(this);
- if (r != JFileChooser.APPROVE_OPTION)
- return;
- File file = chooser.getSelectedFile();
- try {
- FileInputStream filein = new FileInputStream(file);
- progressIn = new ProgressMonitorInputStream(this, "read file "
- + file.getName(), filein);
- final Scanner in = new Scanner(progressIn);
- Runnable readRunnable = new Runnable() {
- public void run() {
- final JTextArea textArea = new JTextArea();
- while (in.hasNextLine()) {
- String line = in.nextLine();
- textArea.append(line);
- textArea.append("/n");
- }
- in.close();
- // set content pane in the event dispatch thread
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- setContentPane(new JScrollPane(textArea));
- validate();
- }
- });
- }
- };
- Thread readThread = new Thread(readRunnable);
- readThread.start();
- } catch (FileNotFoundException e1) {
- e1.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- new TextFrame().showMe();
- }
- }