| Author |
Printing 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.
|
 |
Donny Nadolny
Greenhorn
Joined: Jun 26, 2003
Posts: 7
|
|
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
Joined: Nov 12, 2003
Posts: 15
|
|
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
|
 |
Joe Ess
Bartender
Joined: Oct 29, 2001
Posts: 8291
|
|
|
This is why you aren't supposed to cross post questions. The answer to your compile error is in the other thread.
|
"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
|
 |
john omeara
Greenhorn
Joined: Nov 12, 2003
Posts: 15
|
|
|
Sorry people it was a mistake. I will use the thread in the swing/gui section.
|
 |
 |
|
|
subject: Printing to a text area
|
|
|