Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLConnection and Applets...

 
vbird
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am new to java. It is taking me a while to get the hang of it but I am trying to figure out how to repeatedly read a file in an applet.
I would like to have the applet append the output when new data is found in the text file. I am learning lots on my quest for this bit of information, just not how to do what I want.
I am starting with this very basic example I found somewhere and just want to add the code that would have it do so.
/*
Simpletext Java Applet.
Applet displays a text file from the web in a text area.
The Text Area is as large as the APPLET Width and Height commands.
There is no animation
It loads quickly
And provides information that can be provided quickly.
Utilizes the following Params
<PARAM NAME = "filename" Value = "URL">
<PARAM NAME = "fontname" Value = "Fontname">
<PARAM NAME = "fontsize" Value = "Fontsize">
This is very useful for placing on a page where you want to have information
added without having to touch the HTML in the page. It can also be placed into
a table.

*/
import java.awt.*;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
public class simpletext extends java.applet.Applet implements Runnable
{
URL theURL;
Thread runner;
TextArea ta = new TextArea("Getting text...",7,70);
public void init() {
String fname = getParameter("fontname");
String fsize = getParameter("fontsize");
int fs = Integer.parseInt(fsize);
Font tafont = new Font(fname,Font.PLAIN,fs);
String url = getParameter("filename");
try { this.theURL = new URL(url); }
catch ( MalformedURLException e) {
System.out.println("Bad URL: " + theURL);
}
ta.setEditable(false);
ta.setFont(tafont);
setLayout(new BorderLayout());
add("Center",ta);
// string fnts = getFontList();//not yet implemented in Toolkit
}

public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}

public void stop() {
if (runner != null) {
runner.stop();
runner = null;
}
}
public void run() {
URLConnection conn = null;
DataInputStream data = null;
String line;
StringBuffer buf = new StringBuffer();
try {
conn = this.theURL.openConnection();
conn.connect();
ta.setText("Connection opened...");
data = new DataInputStream(new BufferedInputStream(
conn.getInputStream()));
ta.setText("Reading data...");
while ((line = data.readLine()) != null) {
buf.append(line + "\n");
}
ta.setText(buf.toString());
}
catch (IOException e) {
System.out.println("IO Error:" + e.getMessage());
}
}
}


Each time I try to do a loop or thread I mess up the page or it just doesnt load. Anyone have any helpful ideas??
If you are wondering what I am doing... I have a php chat engine which I have outputted the data to flash but flash sucks for this so I wanted to try it in java. I have successfully set up IRC and some IRC clients but I just want to read a text file in live mode. I would not think it would be so hard to find a good example...
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Each time I try to do a loop or thread I mess up the page or it just doesnt load. Anyone have any helpful ideas??

I would first try flushing and closeing the stream and the conn(as applicable) in the run() method.
regds.
- satya
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"vbird"
your name doesn't agree with the javaranch guidelines.
please take a moment and re-register after reviewing the guidelines at http://www.javaranch.com/name.jsp
thanks for your cooperation.
- satya
 
vbird
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THanks,
I am making some progress... at least I am reading the page live.... I think I can clean this up and have it read new lines only.... The main thing I was looking for was incorporating the loop. Pardon my lack of using the correct terms
/*
Simpletext Java Applet.
Applet displays a text file from the web in a text area.
The Text Area is as large as the APPLET Width and Height commands.
There is no animation
It loads quickly
And provides information that can be provided quickly.
Utilizes the following Params
<PARAM NAME = "filename" Value = "URL">
<PARAM NAME = "fontname" Value = "Fontname">
<PARAM NAME = "fontsize" Value = "Fontsize">
This is very useful for placing on a page where you want to have information
added without having to touch the HTML in the page. It can also be placed into
a table.

*/
import java.awt.*;
import java.io.DataInputStream;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.net.MalformedURLException;
public class simpletext extends java.applet.Applet implements Runnable
{
URL theURL;
Thread runner;
TextArea ta = new TextArea("Getting text...",7,70);
boolean running = true;
public void init() {
String fname = getParameter("fontname");
String fsize = getParameter("fontsize");
int fs = Integer.parseInt(fsize);
Font tafont = new Font(fname,Font.PLAIN,fs);
String url = getParameter("filename");
try { this.theURL = new URL(url); }
catch ( MalformedURLException e) {
System.out.println("Bad URL: " + theURL);
}
ta.setEditable(false);
ta.setFont(tafont);
setLayout(new BorderLayout());
add("Center",ta);
// string fnts = getFontList();//not yet implemented in Toolkit
}

public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}

// public void stop() {
// if (runner != null) {
// runner.stop();
// runner = null;
// }
// }

public void destroy()
{
// will cause thread to stop looping
running = false;
// destroy it.
runner = null;
}

public void run() {
URLConnection conn = null;
DataInputStream data = null;
String line;
StringBuffer buf = new StringBuffer();
while (running)
{
try {
conn = this.theURL.openConnection();
conn.connect();
//ta.setText("Connection opened...");
data = new DataInputStream(new BufferedInputStream(
conn.getInputStream()));
// ta.setText("Reading data...");
//while ((line = data.readLine()) != null) {
line = data.readLine();
buf.append(line + "\n");
// }
ta.setText(buf.toString());
runner.sleep(500);
}
catch (InterruptedException e) {
System.out.println(e);
}

catch (IOException e) {
System.out.println("IO Error:" + e.getMessage());
}
}
}
}

 
vbird
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry my name is Vince Bird... I usually use vbird as my username where ever I go, but will re register under a proper name....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic