[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-09-20
9
Medium Priority
?
556 Views
Last Modified: 2012-09-21
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...
0
Comment
Question by:epifanio67
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 38419137
You'd have to add it to your content pane in that case and hide it when finished.
0
 

Author Comment

by:epifanio67
ID: 38419201
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38419374
You need to add a progress BAR
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:epifanio67
ID: 38419464
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,
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 38419648
I don't have it since the user may open a file 50 - 100 MB..
I'm not sure what your point is. File has length() which will tell you exactly what the max value is
0
 

Author Comment

by:epifanio67
ID: 38420059
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,
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38420118
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.
0
 

Author Closing Comment

by:epifanio67
ID: 38422064
I got it....
:-)

thanks for all your help...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38422114
:)
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
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question