This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Adding text to a text area" Watch "Adding text to a text area" New topic
Author

Adding text to a text area

john omeara
Greenhorn

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");
window.setSize(700,500);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();

TextArea text = new TextArea("Network packets will he here",18, 30, TextArea.SCROLLBARS_NONE);
panel.add(text);
window.getContentPane().add(panel);
window.show();



}
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){
System.out.println(packet);
}
public static void main(String[] args) throws java.io.IOException{
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
Bartender

Joined: Oct 29, 2001
Posts: 8834
    
    7

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.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Adding text to a text area
 
Similar Threads
Jpcap captured packets not appearing in Swing application's text area
Printing to a text area
How can I use the Class JpcapCaptor loopPacket method without blocking my Windows app
Wordwrap on a JTextArea
Populating JList in Parent window from Child window's save button