Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

ProgressMonitorInputStream test

Thread view: 
Andrew Thompson - 27 Sep 2007 04:35 GMT
ProgressMonitorInputStream: "..a progress monitor to monitor
the progress of reading the input stream. If it's taking a
while, a ProgressDialog will be popped up to inform the user."

Sounds neat, huh?  Wrap a plain old Inputstream in one of
these, and the user is kept informed.  But I cannot get
it to work!

The code (below) uses it, and offers two URL's, the first
being the entire class list of the Java 6 API, the second
being the trivially small content of the frames based
index page (it would be large if the JEP was interpreting
the input as HTML, but it is displayed as plain text).

Yet while loading the first URL here, I get:-

Wed Sep 26 22:14:42 ACT 2007    Start load
Wed Sep 26 22:18:54 ACT 2007    End load

Over 4 minutes duration, but no appearance of the progress
dialog!

What does 'a while' mean?
Does this code work (pop a progress dialog) for you?
Assuming 'no' for the previous question.
Are there variants of this code that *do* pop the dialog?

If you have an very fast connection, try setting
the URL to point at something much bigger, the
JComboBox is editable.

Thoughts, comments, bug report URLs and solutions
all welcome.

<sscce>
// this is what we are testing..
import javax.swing.ProgressMonitorInputStream;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JComboBox;
import javax.swing.SwingUtilities;

import java.io.IOException;
import java.io.InputStream;

import java.net.URL;
import java.net.MalformedURLException;

import java.util.Date;

class TestStreamProgress extends JFrame {

 JEditorPane output;

 TestStreamProgress() throws MalformedURLException {
   super("ProgressMonitorInputStream - Test");
   output = new JEditorPane();
   output.setText("Choose a resource to load");

   this.getContentPane().add(
     new JScrollPane(output)
     );
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   pack();
   setSize(600,400);
   setLocationRelativeTo(null);
   setVisible(true);

   String[] resourceLocator = {
     "http://java.sun.com/javase/6"+
     "/docs/api/allclasses-frame.html",
     "http://java.sun.com/javase/6/"+
     "docs/api/index.html", // a small resource..
     "other (type URL)"
   };
   JComboBox resourceSelector =
     new JComboBox(resourceLocator);
   resourceSelector.setEditable(true);
   JOptionPane.showMessageDialog(null, resourceSelector);
   String resource =
     (String)resourceSelector.getSelectedItem();
   URL url = new URL( resource );
   loadResource(url);
 }

 public void loadResource(final URL location) {
   output.setText("Loading..");
   Thread t = new Thread() {
     public void run() {
       try {
         final InputStream is = location.openStream();
         ProgressMonitorInputStream pmis =
           new ProgressMonitorInputStream(
             output,
             "Loading..",
             is
             );
         System.out.println( new Date() +
           " \tStart load" );
         output.read( pmis, null );
         System.out.println( new Date() +
           " \tEnd load" );
       } catch(Exception e) {
         e.printStackTrace();
       }
     }
   };
   SwingUtilities.invokeLater(t);
 }

 public static void main(String[] args)  {
   Thread t = new Thread() {
     public void run() {
       try {
         TestStreamProgress tsp =
           new TestStreamProgress();
       } catch(MalformedURLException e) {
         e.printStackTrace();
       }
     }
   };
   SwingUtilities.invokeLater(t);
 }
}
</sscce>

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Andrew Thompson - 27 Sep 2007 05:36 GMT
>ProgressMonitorInputStream:
...
>Thoughts, comments, bug report URLs ...

A search of the bug DB on ProgressMonitorInputStream
shows the following hits:

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4075058>
Mentioned as an example in support of interruptible streams.

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4140706>
One string in a list data object, not otherwise used.

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4209604>
Seems to have been used inside the EDT.  
Solution - don't block the EDT.  'Closed, will not be fixed'.

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6456237>
"Remove synchronized keyword from method declarations in Swing"
PMIS is listed as one of the classes that uses it.  I cannot
see how removing the synchronized keyword would fix this,
but I might be wrong.

<http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5089193>
"JTextArea.setText() wastes megabytes of memory!"
Uh-huh.  This is a little more informative in that it gets
the underlying ProgressMonitor* and adjusts the params.  
I don't know how I missed the getProgressMonitor()
method!  (Except of course, the obvious answer that I
am a moron who does not take enough care to RTFM.)
Will check this out further.
(though the actual bug report does not relate to PMIS,
and is closed as a duplicate).

* This has answered one of my questions, namely..

>What does 'a while' mean?

2.5 seconds is the default.  (2 secs. pass before it even
considers appearing, then, if the read is expected to take
less than half a second, it will not pop up).

These questions are still open..

>Does this code work (pop a progress dialog) for you?
>Assuming 'no' for the previous question.
>Are there variants of this code that *do* pop the dialog?

..continuing the investigation.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Andrew Thompson - 27 Sep 2007 14:25 GMT
>>ProgressMonitorInputStream:
>...

Finally thought to trawl through the Java Tutorial and
see what it had to say about the PMIS.

The tutorial examples work nicely.  They put the 'long'
task into a sub-class of SwingWorker and implement
propertyChangeListeners in the GUI.  Quite different to
the way I was going about it.

This thread is resolved.

Final Score:
Java Tutorial:  1  |  Me:  0

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Roedy Green - 27 Sep 2007 23:22 GMT
>ProgressDialog will be popped up to inform the user."
I have not used that particular feature, but I got progress bars to
work fine using the other features.
See http://mindprod.com/jgloss/progress.html
To see it ins a real life situation, download
http://mindprod.com/products1.html#REPLICATOR
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Andrew Thompson - 28 Sep 2007 04:56 GMT
>>ProgressDialog will be popped up to inform the user."
>I have not used that particular feature, but I got progress bars to
>work fine using the other features.
...
Yeah, I had previously used a JProgresBar in The Giffer
with good effect.  The advantage is that I was accessing
the API at a slightly 'lower' level than is offered by the
ProgressMonitorInputStream.  The technique I outlined
in the first post works* for JPB.

* With a few additional lines to manually update
the progress.

Thanks for your attention, and the links.

Signature

Andrew Thompson
http://www.athompson.info/andrew/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.