• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Printing to a text area

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey. You could add in a method to Serverside1 to return the text area that is displayed. Then any class you wanted could draw to it. For example:
import java.awt.*;
class c1 {
TextArea txt;
public c1() {
txt = new TextArea();
}
public TextArea getText() {
return txt;
}
}

public class c2 {
public static void main(String[] args) {
c1 myClass1 = new c1();
TextArea txt = myClass1.getText();
myClass1.append("hello");
}
}

This code won't make a frame for the text area and won't display anything (I have to go to school now, it's simple to make it add the textarea to a frame though). Hope this helps.
 
john omeara
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers for the reply Donny.
I have done what you have suggested - the logic to it sounds right, but when i complile by i get an error - here is the new code for serverside1
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Serverside1 extends JFrame
{
private JFrame window;
private MenuBar bar;
private MenuItem about;
private Menu help;
public Serverside1()
{

// Create frame and panels for GUI
window = new JFrame("Network Packet Sniffer");
window.setSize(700,500);
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
GridLayout gridLayout = new GridLayout(2,1);
panel.setLayout(gridLayout);
// Add textarea for packet capture diaplay
TextArea text = new TextArea("Packet Capture Off",18, 30, TextArea.SCROLLBARS_NONE);
panel.add(text);
window.getContentPane().add(panel);
window.show();

bar = new MenuBar();
window.setMenuBar(bar);

about = new MenuItem("About");
help = new Menu("Help");
help.add(about);
bar.add(help);
window.setVisible(true);

}
public TextArea getText(){
return text;
}

public static void main(String[] args){
Serverside1 s = new Serverside1();
}
}
the error is below

C:\Project>javac Serverside1.java
Serverside1.java:51: cannot resolve symbol
symbol : variable text
location: class Serverside1
return text;
^
1 error
will i also need to implement any classes in serverside1.
Again thanks for the help
 
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is why you aren't supposed to cross post questions. The answer to your compile error is in the other thread.
 
john omeara
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry people it was a mistake. I will use the thread in the swing/gui section.
 
    Bookmark Topic Watch Topic
  • New Topic