File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Adding text to a text area Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding text to a text area" Watch "Adding text to a text area" New topic

Adding text to a text area

john omeara

Joined: Nov 12, 2003
Posts: 15
Hi folks
I am returning to java after a bit of a time away so if someone could help me with this little query it would be appreciated. Below are two sets of code. The first draws a simplegui with a textarea. The second is a piece of code given to me to capture packets of the network card and displays them in a terminal.
If anyone knows how it will be possible so that the packets are printed to the gui text area by ammending the code it would be most helpful.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Serverside1 extends JFrame
private JFrame window;
public Serverside1()
window = new JFrame("Network Packet Sniffer");
JPanel panel = new JPanel();

TextArea text = new TextArea("Network packets will he here",18, 30, TextArea.SCROLLBARS_NONE);

public static void main(String[] args){
Serverside1 s = new Serverside1();

Code for packet capture
import jpcap.*;
class Tcpdump implements JpcapHandler
public void handlePacket(Packet packet){
public static void main(String[] args) throws{
String[] lists=Jpcap.getDeviceDescription();
System.out.println("Start capturing on "+lists[0]);
Jpcap jpcap=Jpcap.openDevice(Jpcap.getDeviceList()[0],1000,false,20);
jpcap.loopPacket(-1,new Tcpdump());

Thanks a lot folks.
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Easy. You need to keep a reference to your text area somewhere so you can get to it. I'd recommend declaring it first thing in the class. That gives the entire instance of Serverside1 access to it.
Next you need to append the text. There's a couple of ways you can do it. First, you could have Serverside1 implement JpcapHandler. That would be easiest, because you can just call text.append() in Serverside1's handlePacket method. The other way to do it is to have an inner class like you do now and pass it a reference to either Serverside1 or the text area, and use that reference in the inner class' handlePacket method.

[How To Ask Questions On JavaRanch]
I agree. Here's the link:
subject: Adding text to a text area
It's not a secret anymore!