Computing.Net > Forums > Programming > Help with java gui

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Help with java gui

Reply to Message Icon

Name: John O'Meara
Date: November 13, 2003 at 07:41:54 Pacific
OS: 2k
CPU/Ram: p2/128
Comment:

Hi folks if anyone can help me with this it would be appreciated as its been a while since i have programmed

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 off the network card and displays them in a terminal.

If anyone knows how it will be possible so that when the start button on my application is pressed the packets are printed to the gui text area. I have added an actionlistener to the button but i am unsure of the next step. i have been told one option is to have my program implement jpcap handler but as i havent programmed for a while im struggling with syntax. If anyone has suggestions they would be appreciated. Heres my code any how:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import jpcap.*;

public class Servercapture extends JFrame implements ActionListener
{
private JFrame window;
private MenuBar bar;
private MenuItem about;
private Menu help;
private MenuItem start;
private Menu capture;

public Servercapture()
{

// 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");
start = new MenuItem("Start");
start.addActionListener(this);
capture = new Menu("Capture");
capture.add(start);
bar.add(capture);
help.add(about);
bar.add(help);

window.setVisible(true);

}


public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if (source == start)
{

}

}

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

********************************************************************

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 folks



Sponsored Link
Ads by Google

Response Number 1
Name: Gagey
Date: November 17, 2003 at 19:29:13 Pacific
Reply:

I just combined your 2 classes, with some minor mods to Servercapture (TextArea is now member variable, NOT local to constructor), implements JpcapHandler, and loopPacket (in menu handler) is passed 'this' as the Handler object.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import jpcap.*;

public class Servercapture extends JFrame implements ActionListener, JpcapHandler {

private JFrame window;
private MenuBar bar;
private MenuItem about;
private Menu help;
private MenuItem start;
private Menu capture;
private TextArea text;

public Servercapture()
{

// 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
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");
start = new MenuItem("Start");
start.addActionListener(this);
capture = new Menu("Capture");
capture.add(start);
bar.add(capture);
help.add(about);
bar.add(help);

window.setVisible(true);

}


public void actionPerformed(ActionEvent e){
Object source = e.getSource();
if (source == start)
{
try {
String[] lists=Jpcap.getDeviceDescription();
text.setText("Start capturing on "+lists[0]+"\n");
Jpcap jpcap=Jpcap.openDevice(Jpcap.getDeviceList()[0],1000,false,20);
jpcap.loopPacket(-1,this);
}
catch (java.io.IOException ex) {
System.err.println("Error: " + e) ;
}
}
}

public void handlePacket(Packet packet){
text.append(packet.toString() + "\n");
}

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


It works when I run it to monitor PPP adapter, so I don't see why it wouldn't work with NIC.



0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Help with java gui

Help with java input www.computing.net/answers/programming/help-with-java-input/8713.html

Need some help with java. www.computing.net/answers/programming/need-some-help-with-java/2134.html

I need help with java! www.computing.net/answers/programming/i-need-help-with-java/5990.html