Link to home
Start Free TrialLog in
Avatar of epifanio67
epifanio67

asked on

Java: how do I keep my ProgressMonitorInputStream obj from popping on new window/frame?

Hello Experts,

how do I keep my ProgressMonitorInputStream obj from popping on new window/frame?

	static final int MY_MINIMUM = 0;
	static final int MY_MAXIMUM = 100;
	JScrollPane scroll;
	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();
		scroll = new JScrollPane(textArea);
		getContentPane().add(scroll, BorderLayout.CENTER);
		
		pbar = new JProgressBar();
		progressBarPanel.add(pbar);
				
		progressBarPanel = new JPanel();
		
//		progressBarPanel.add(pbar);		
		getContentPane().add(progressBarPanel, BorderLayout.SOUTH);		
		
		openFile();	
		

	}//END OF CONSTRUCTOR



	private void openFile() {
		
		try {
			
			FileInputStream fileIn = new FileInputStream(fileName); 
			ProgressMonitorInputStream progressIn = new ProgressMonitorInputStream(
					this, "", fileIn);
			final Scanner in = new Scanner(progressIn);
			
			textArea.setText("");

			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

				protected Void doInBackground() throws Exception {

					while (in.hasNextLine()) {
						String line = in.nextLine();
						textArea.append(line);
						textArea.append("\n");
					}
					in.close();
					return null;
				}
			};
			worker.execute();
			

		} 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


the code works ok... but instead of providing progress monitoring on small window... I want to add to progressBarPanel in the main window.... how to do that?

see what I mean?

In advance, thank you for your help...
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You'd have to add it to your content pane in that case and hide it when finished.
Avatar of epifanio67
epifanio67

ASKER

Thank you CEHJ,

I tried that... but it didn't let me...
ProgressMonitorInputStream is not a component....

any other ideas?

Thanks for your help...

Regards
You need to add a progress BAR
Thanks CEHJ,

hmmm.... well, the JProgressBar obj requires a max...
I don't have it since the user may open a file 50 - 100 MB..

so, not sure how to incorporate ProgressMonitorInputStream obj and JProgressBar obj... any ideas?

the ProgressMonitorInputStream does show the user progress as the file opens but it does in small frame.... I want to see if it is possible to add the progress provided by  ProgressMonitorInputStream to my main app...

Thanks CEHJ... I really appreciate your help...

Regards,
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you CEHJ for your help....

hmmm... still not working...
please take a look at updated code:

public class SwingProgressBarExample extends JFrame {

	private static final long serialVersionUID = 1L;
	static final int MY_MINIMUM = 0;
	static final int MY_MAXIMUM = 100;
	JScrollPane scroll;
	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();
		scroll = new JScrollPane(textArea);
		getContentPane().add(scroll, BorderLayout.CENTER);
		
		pbar = new JProgressBar();
		pbar.setMinimum(0);
		pbar.setStringPainted(true);
		pbar.setVisible(true);
		progressBarPanel = new JPanel();
		
		progressBarPanel.add(pbar);		
		getContentPane().add(progressBarPanel, BorderLayout.SOUTH);		
		
		openFile();	
		

	}//END OF CONSTRUCTOR



	private void openFile() {
		
		try {
						
			FileInputStream fileIn = new FileInputStream(fileName);
			
			pbar.setMaximum(fileIn.available());
			
			ProgressMonitorInputStream progressIn = new ProgressMonitorInputStream(
					this, "", fileIn); 
			final Scanner in = new Scanner(progressIn);
						
			textArea.setText("");
			
			SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

				protected Void doInBackground() throws Exception {

					while (in.hasNextLine()) {
						String line = in.nextLine();
						textArea.append(line);
						textArea.append("\n");
						pbar.setValue(line.length());
					}
					in.close();
					return null;
				}
				
			};	
			worker.execute();				
			
		} 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


any thoughts / suggestions?

Regards,
I would suggest you take a step back from this and study SwingWorker a bit before you start coding this, as you have an extra complication. You want to append text while the file reading is still going on. That means you need to use SwingWorker.process to do that appending. Read the excellent API docs for SwingWorker as it has an example of just that.
Leave the progress monitoring out of it for now and just get the appending working. Come back when you've done that.
I got it....
:-)

thanks for all your help...
:)
Incidentally, for a meaningful interpretation of 'progress' for your use case, you'd have to find out how many lines there are in the file first and use that as your maximum value. otoh, you might consider the actual appearance of each consecutive line visual feedback enough