• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1371
  • Last Modified:

java: how would I add progress bar to my swing app while waiting for large file to open?

Hello Experts,

how would I add progress bar to my swing app while waiting for large file to open?

public class SwingProgressBarExample extends JFrame {

	private static final long serialVersionUID = 1L;
	static final int MY_MINIMUM = 0;
	static final int MY_MAXIMUM = 100;
	StringBuffer stringBuffer;
	String log;

	String fileName = "large.log";//50MB
	
	JProgressBar pbar;
	private JPanel progressBarPanel;
	private JTextArea textArea;
	
	int lineNumber = 0;

	//CONSTRUCTOR
	public SwingProgressBarExample() {		
		
		setTitle("Progress Bar Example...");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(600, 400);
		
		textArea = new JTextArea();
		getContentPane().add(textArea, BorderLayout.CENTER);
				
		progressBarPanel = new JPanel();
		
		pbar = new JProgressBar();
		pbar.setMinimum(MY_MINIMUM);
		pbar.setMaximum(MY_MAXIMUM);
		
		progressBarPanel.add(pbar);		
		getContentPane().add(progressBarPanel, BorderLayout.SOUTH);		
		
		openFile();		

	}//END OF CONSTRUCTOR



	private void openFile() {
		
		try {
			FileReader fileReader = new FileReader(fileName);
			BufferedReader br = new BufferedReader(fileReader);
			stringBuffer = new StringBuffer();	
			String line;	

			while ((line = br.readLine()) != null) {
				lineNumber++;
				stringBuffer.append(line + "\n");
				pbar.setValue(lineNumber);
			}
			fileReader.close();
			log = stringBuffer.toString();
			textArea.setText(log);

		} catch (IOException ioe) {
			System.out.println("IOException: " + ioe);
			ioe.printStackTrace();
		}
		
	}

	
	public static void main(String args[]) {

		final SwingProgressBarExample it = new SwingProgressBarExample();
		it.setVisible(true);

	}
}

Open in new window


In advance, thank you for your help...
0
epifanio67
Asked:
epifanio67
  • 2
1 Solution
 
HonorGodCommented:
You'll need to add a SwingWorker instance that generates (produces) events

Java Swing Tutorial: How to Use Progress Bars
0
 
epifanio67Author Commented:
thank you expert...

Regards,
0
 
HonorGodCommented:
Thanks for the grade & points.

Good luck & have a great day.

If you want / need additional assistance, don't be afraid to ask.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now