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 / GUI / December 2003

Tip: Looking for answers? Try searching our database.

How to assign a string buffer to a JEditorPane

Thread view: 
Martin Unzner - 26 Dec 2003 10:12 GMT
Hello,

I wanted to program a little Browser. For viewing the HTML, I would use
a JEditorPane. That component has got a method setPage(URL), but I don't
want to use it because it doesn't work with all sites in the WWW (even
the java.sun.com page isn't displayed). So I wanted to load the site
into a string buffer with an algorithm like this:

   void getData(URL url) {
       URLConnection conn = null;
       InputStreamReader in;
       BufferedReader data;
       String line;
       StringBuffer buf = new StringBuffer();
       try {
           conn = url.openConnection();
           conn.connect();
           box.setText("Connection opened ...");

           in = new InputStreamReader(conn.getInputStream());
           data = new BufferedReader(in);

           box.setText("Reading data ...");
           while ((line = data.readLine()) != null)
               buf.append(line + "\n");
       } catch (IOException e) {
           System.out.println("IO Error:" + e.getMessage());
       }
   }

But my question is: How do I assign this buffer with the JEditorPane and
set the Content Type HTML? I tried some examples from this group. They
worked if I used a string, but they didn't work with the buffer
converted to a string. I don't understand that and I think there are
better ways than converting a buffer to a string and then setting it.
Can anybody help me?

Thanks.

Martin Unzner
Andrew Thompson - 26 Dec 2003 16:59 GMT
...
> I wanted to program a little Browser.

Various others have thought that was a good
idea too.  I have one of my own.

> For viewing the HTML, I would use
> a JEditorPane. That component has got a method setPage(URL), but I don't
> want to use it because it doesn't work with all sites in the WWW (even
> the java.sun.com page isn't displayed).

Unfortunately the JEditorPane is a little
'challenged' - I gave up as soon as I saw
how long it took to load the main page
of the API docs from the local filesystem.

Far too long.

And then there is it's rendering, ..I won't go there.

>..So I wanted to load the site
> into a string buffer with an algorithm like this:
[quoted text clipped - 25 lines]
> worked if I used a string, but they didn't work with the buffer
> converted to a string.

If you could get the ones from the _group_
to work it means it must be a problem in how
_you_ are doing it.  Now you've been rather
unhelpful in providing a code snippet, rather than
a SSCCE, but I do notice something odd about
the snippet.

You do not seem to be _doing_ anything with
the StringBuffer once it's loaded, you do not
return it from this 'void' method, and you do
not assign the data to anything else prior to
_ending_ the method.  So.. how did you expect
the data to get from the SB to the JEP?

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Martin Unzner - 27 Dec 2003 07:44 GMT
> If you could get the ones from the _group_
> to work it means it must be a problem in how
[quoted text clipped - 9 lines]
> _ending_ the method.  So.. how did you expect
> the data to get from the SB to the JEP?

I didn't take this code from a newsgroup, and it doesn't end here.
Here is the whole code (box is the JEditorPane):

   void getData(URL url) {
       URLConnection conn = null;
       InputStreamReader in;
       BufferedReader data;
       String line;
       StringBuffer buf = new StringBuffer();
       try {
           conn = url.openConnection();
           conn.connect();
           box.setText("Connection opened ...");

           in = new InputStreamReader(conn.getInputStream());
           data = new BufferedReader(in);

           box.setText("Reading data ...");
           while ((line = data.readLine()) != null)
               buf.append(line + "\n");

       box.setContentType ("text/html");
       Document doc = box.getDocument ();
            EditorKit kit = box.getEditorKit ();

       String html = buf.toString();
       StringReader reader = new StringReader (html);
       kit.read (reader, doc, 0);
        } catch (IOException e) {
           System.out.println("IO Error:" + e.getMessage());
       }
   }

Something other I tried is this :

   void getData(URL url) {
       URLConnection conn = null;
       InputStreamReader in;
       BufferedReader data;
       String line;
       StringBuffer buf = new StringBuffer();
       try {
           conn = url.openConnection();
           conn.connect();
           status_label.setText("Connection opened ...");

            box.setContentType ("text/html");
            Document doc = box.getDocument ();
            EditorKit kit = box.getEditorKit ();

           in = new InputStreamReader(conn.getInputStream());
           data = new BufferedReader(in);
           status_label.setText("Reading data ...");
              kit.read (data, doc, 0);
       } catch (Exception e) {
           status_label.setText("Error:" + e.getMessage());
       }
   }

And now my question is: What do I have to change? Or what other piece
of code should I use?

Thanks.

Martin Unzner
hiwa - 27 Dec 2003 04:43 GMT
> load the site into a string buffer
That's simply ineffective.
Martin Unzner - 27 Dec 2003 08:00 GMT
hiwa schrieb:

>>load the site into a string buffer
>
> That's simply ineffective.

Propose another solution, please.

Martin Unzner
Andrew Thompson - 27 Dec 2003 12:26 GMT
> > load the site into a string buffer

> That's simply ineffective.

The OP was talking about an URL,
they meant a web _page_ rather than
a web _site_.
And yes, it is certainly doable.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
hiwa - 28 Dec 2003 00:37 GMT
> > groups.5.unzi@spamgourmet.com (Martin Unzner) wrote in message
>  news:<5598271c.0312260212.680b9038@posting.google.com>...
[quoted text clipped - 6 lines]
> a web _site_.
> And yes, it is certainly doable.
Yes, doable. But it's ineffective. Try yourself.
The bottom line is: you can't use Java JEditorPane to implement even a
quasi-satisfactory Web browser.
Andrew Thompson - 28 Dec 2003 10:09 GMT
> > > groups.5.unzi@spamgourmet.com (Martin Unzner) wrote in message
> >  news:<5598271c.0312260212.680b9038@posting.google.com>...
[quoted text clipped - 7 lines]
> > And yes, it is certainly doable.
> Yes, doable. But it's ineffective. Try yourself.

Yes. yes.  I _am_ familiar w/JEditorPane
and its failings, it was not clear what exactly
you meant (it sounded like you might have breen
referring to the StirngTokenizer), as just what
you mean by 'ineffective'.

I would agree that I would not use a JEditorPane
If I  had a choice.  However, if you were in a
situation where you had no alternative, you
could certainly make a JEditorPane based
UI to browse to (basic) web pages and obtain
information.

And yes, I do know because I _have_
done it.

Your defintition of 'ineffective' may mean
'sub optimal', but I argue it means 'not fit
for the purpose' - if your pusprose is getting
information from basic web pages -
JEditorPane certainly _is_ effective.

> The bottom line is: you can't use Java JEditorPane to implement even a
> quasi-satisfactory Web browser.

Now you are being clearer, but I think
it is important to make a distintion.  IF
the OP was talking about displaying a
web page written by themsleves as
'help' for their program.
A JEditorPane would be quite sufficient.

For general browsing on the internet,
you would almost certainly choose
something else.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
hiwa - 29 Dec 2003 02:01 GMT
> > "Andrew Thompson" <andrew64@bigNOSPAMpond.com> wrote in message
>  news:<kUeHb.66679$aT.59874@news-server.bigpond.net.au>...
[quoted text clipped - 45 lines]
> you would almost certainly choose
> something else.

Oh. Please don't be so harsh. I just meant that reading a String or
StringBuffer from socket input stream is ineffective for viewing a web
page compared to the standard setPage(URL) method. setPage() is usable
for simple pages.
Andrew Thompson - 29 Dec 2003 09:02 GMT
..yada, yada, (yada)
> > A JEditorPane would be quite sufficient.
...
> Oh. Please don't be so harsh.

Ehhh?

Insistent - maybe,
stubborn - ..probably,
but harsh?

That was not my intent.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site
Martin Unzner - 29 Dec 2003 15:33 GMT
Andrew Thompson schrieb:

>>"Andrew Thompson" <andrew64@bigNOSPAMpond.com> wrote in message
>
[quoted text clipped - 20 lines]
> * http://www.1point1C.org/ 1.1C - Superluminal!
> * http://www.AThompson.info/andrew/ personal site

Can you please answer my question: What is more effective than a string
buffer and what do I have to use if not a JEditorPane? Should I use a
JPanel and then tip down all html-tags to filter them?

Thanks.
Martin Unzner
Andrew Thompson - 30 Dec 2003 03:23 GMT
Hi Martin!

This thread has gone all over the place,
has it not?!

Ultimately I feel the answers you are getting
are saying something more fundamental,
something you have missed.

Perhaps you should take
a step back and think about..
a) where does the information start?
b) what do you want to do with it?

Now I don't know (a), but could guess
(b) is 'get information to user'.

You asked about JEditorPanes (bery
restrictive) when you really _seem_ to
just want to provide info. to the user.
There may be better ways.

The Stringbuffer itself is also, without
a doubt, irrelevant (you may notice I
convert the SB to a string before the
final assignation below).

So I am going to ask you to take a step
(or two) back from your code and ask
yourself the questions above - see if what
you are doing still makes sense.

Having said that..
http://www.physci.org/launcher.jsp
follow the link to JAddPane (which is)
http://www.physci.org/launcher.jsp?class=/codes/HTMLOP/JAddPane

Gives both the example and demo.

HTH

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


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



©2008 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.