How to implement pipe connectors, like Yahoo pipes, using Java Swing. Or any other kind of wiring in Java Swing, for that matter.
Asked
Active
Viewed 490 times
1 Answers
5
Here is an example of using PipedReader
and PipedWriter
in java:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedReader;
import java.io.PipedWriter;
/**
* @date 1st May, 2011
* @author sanju.org
*
* To demonstrate the use of piped character streams.
* Example program for piped character stream in java.
*
*/
public class PipedReaderExample {
public static void main(String args[]) throws IOException{
PipedReader reader = new PipedReader();
PipedWriter writer = new PipedWriter(reader);
Thread readerThread = new Thread(new ReaderThread(writer));
Thread writerThread = new Thread(new WriterThread(reader));
readerThread.start();
writerThread.start();
}
}
class ReaderThread implements Runnable{
PipedWriter writer;
public ReaderThread(PipedWriter writer){
this.writer = writer;
}
public void run() {
InputStreamReader streamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(streamReader);
try {
while (true) {
//sample implementation reading from console
//real implementation can be reading from a socket or a file
//or from server side code
String line = bufferedReader.readLine();
writer.write(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class WriterThread implements Runnable{
PipedReader reader;
public WriterThread(PipedReader reader){
this.reader = reader;
}
public void run() {
while(true){
try {
char c;
while( (c = (char)reader.read()) != -1){
//write your business logic here
//could be writing into a file
//could be processing the date
System.out.println(c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
As for using yahoo with PipedReader/Writer
class I guess you'd read their API to connect to url etc
Here is a list of other links relating to PipedReader
and PipedWriter
(mix of tutorials and information) that could be of help:
- Java IO: Pipes
- Java IO: PipedReader
- PipedReader PipedWriter example (JavaCodeGeeks exmaple)
- PipedReader and PipedWriter and thread (Java2s example)
As for the Swing side of things, to display the data received from a pipe you would need to use a TextComponent
like one of these:
each have a setText(...)
which will allow you to set their content
Reference:

David Kroukamp
- 36,155
- 13
- 81
- 138
-
1+1 WOW nice! Might like to add some info about concurrency in Swing though ;) – MadProgrammer Oct 23 '12 at 21:50
-
1+1 See also this related [example](http://stackoverflow.com/a/4444677/230513). – trashgod Oct 24 '12 at 00:41
-
@MadProgrammer an trashgod thank you and +1 to both (Pipes are very rare hey?!) – David Kroukamp Oct 24 '12 at 18:01