Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Displaying the contents of a URL within a TextArea

Status
Not open for further replies.

FAM

Technical User
Jan 13, 2003
345
0
0
GB
Im trying to read the contents of a url from a Dialog box into a TextArea called field within my main menu, the Dialog box returns urlChoice i.e.
Code:
  public String getUrlInput()
  {
    return urlChoice;
  }
this is part of the code within my mainmenu
Code:
        private String url;

  	if (command.equals("connect"))
  	{
  	URLDialog window = new URLDialog(this, "URL dialog", true);
        url = window.getUrlInput();	
	field.append("\nThe URL Selected is " + url); //Works fine to here
The next part is where the problems start
Using the example,
Code:
		//URL url1 = new URL(url);
		BufferedReader in = new BufferedReader(
				new InputStreamReader(
				url.openStream()));

		String inputLine;

		while ((inputLine = in.readLine()) != null)
	    System.out.println(inputLine);

		in.close();
When this code is compiled it has one error saying,
cannot find symbol method openStream()
url.openStream()));
^

Any suggestions to output the url contents into field?
Cheers
 
Here is an example
Code:
class Hyperactive implements HyperlinkListener {
 
         public void hyperlinkUpdate(HyperlinkEvent e) {
             if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                 JEditorPane pane = (JEditorPane) e.getSource();
                 if (e instanceof HTMLFrameHyperlinkEvent) {
                     HTMLFrameHyperlinkEvent  evt = (HTMLFrameHyperlinkEvent)e;
                     HTMLDocument doc = (HTMLDocument)pane.getDocument();
                     doc.processHTMLFrameHyperlinkEvent(evt);
                 } else {
                     try {
                         pane.setPage(e.getURL());
                     } catch (Throwable t) {
                         t.printStackTrace();
                     }
                 }
             }
         }
     }

If you still not sure about it, look the following website.



Chinese Java Faq Forum
 
I wrote an example:

Code:
url = new URL("[URL unfurl="true"]http://www.4ugo.com/bbs/index.asp");[/URL]
InputStream in = url.openStream();
jEditorPane.read(in,null);

Chinese Java Faq Forum
 
I think FAM's code should work after uncommenting the line where URL1 is defined and use it to get the stream.

Cheers,
Dian
 
Thanks for the help but after more issues i tried a slightly different approach and got it working with,
Code:
  	if (command.equals("connect"))
  	{
  		URLDialog window = new URLDialog(this, "URL dialog", true);
        url = window.getUrlInput();	

		DataInputStream file = null;
		try
		{
			//field.append("\nThe URL Selected is " + url);
			file = new DataInputStream((new URL(url)).openStream());
				
			url = file.readLine();
			while (url != null)
			{
				field.append(url);
				url = file.readLine();
			}
		}
		catch (IOException e)
		{
			System.out.println("Error : " + e);
		}
  	}
Thanks for the suggestions
 
Glad you got it to work.

As an advice, it's not a good practice to reuse variables for so differente purposes. The url you declare, that I guess is a String is used afterwards to store a line readed from a field.

I know it works but it makes really difficult to read it and understand what's really happening.

Cheers,
Dian
 
Dian, what do you suggest to make it easier to read?
Create a new String variable?

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top