Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple gui

Status
Not open for further replies.

johno77

Technical User
Nov 1, 2003
40
GB
Hello

I am looking at putting the code belowinto a graphical user interface so that when it is run rather than printing to the command line it prints out to a simple text text box in the gui. The code basically put the nic card into promiscuos mode and prints out all the packets it receives -I amlooking to develop it further in the future to add functionalty but i would be grateful if someone could point me in the right direction to get going.

Heres the code so far

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());
}

}

many thanks
 
If you want to print simple text to text box, you should use awt or Swing.
System.out.println() will print in the dos prompt or where you type the command.
 
I think the following is exactly what you need. It contains a main. Just compile and run it. In your case : Instead of using "System.out.println(...)" use "appendText(String text)" instead.
A full working demo from "Core Swing Advanced Programming" by Kim Topley.
The method that does the magic is "scrollRectangleToVisible(...)". Since this is a method from JComponent is will also work for a JEditorPane.
Code:
package com.ecc.swing2;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Date;

public class AppendingTextPane extends JTextPane {
    public AppendingTextPane() {
        super();
    }

    public AppendingTextPane(StyledDocument doc) {
        super(doc);
    }

    // Appends text to the document and ensure that it is visible
    public void appendText(String text) {
        try {
            Document doc = getDocument();

            // Move the insertion point to the end
            setCaretPosition(doc.getLength());

            // Insert the text
            replaceSelection(text);

            // Convert the new end location
            // to view co-ordinates
            Rectangle r = modelToView(doc.getLength());

            // Finally, scroll so that the new text is visible
            if (r != null) {
                scrollRectToVisible(r);
            }
        } catch (BadLocationException e) {
            System.out.println("Failed to append text: " + e);
        }
    }

    // Testing method
    public static void main(String[] args) {
        JFrame f = new JFrame("Text Pane with Scrolling Append");
        final AppendingTextPane atp = new AppendingTextPane();
        f.getContentPane().add(new JScrollPane(atp));
        f.setSize(200, 200);
        f.setVisible(true);

        // Add some text every second
        Timer t = new Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                String timeString = fmt.format(new Date());
                atp.appendText(timeString + "\n");
            }

            SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
        });
        t.start();
    }
}
 
Cheers hologram

I have tried conpiling the code you gave me before i make adjustments but the following errors have occured. Am i possibly missing some libraries?

c:\temp java AppendingTextPane

Exception in thread "main" java.lang.NoClassDefFoundError: AppendingTextPane (wr
ong name: com/ecc/swing2/AppendingTextPane)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)


cheers
 
Silly question I know but have you compiled it using ...

javac AppendingTextPane.java


??????
 
If you are working without packages (in the "unnamed package") , then remove the first line and try again.

package com.ecc.swing2; // REMOVE THIS LINE

 
Cheers guys that seems to have done the trick all though i am having problem getting to grips with some of the java as its been a while since ive touched it. Heres my code now



import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Date;
import jpcap.*;

public class AppendingTextPane extends JTextPane {
public AppendingTextPane() {
super();
}

public AppendingTextPane(StyledDocument doc) {
super(doc);
}

// Appends text to the document and ensure that it is visible
public void appendText(String text) {
try {
Document doc = getDocument();

// Move the insertion point to the end
setCaretPosition(doc.getLength());

// Insert the text
replaceSelection(text);

// Convert the new end location
// to view co-ordinates
Rectangle r = modelToView(doc.getLength());

// Finally, scroll so that the new text is visible
if (r != null) {
scrollRectToVisible(r);
}
} catch (BadLocationException e) {
System.out.println("Failed to append text: " + e);
}
}


// Testing method
public static void main(String[] args)throws java.io.IOException {
JFrame f = new JFrame("Text Pane with Scrolling Append");
final AppendingTextPane atp = new AppendingTextPane();
f.getContentPane().add(new JScrollPane(atp));
f.setSize(200, 200);
f.setVisible(true);


String[] lists=Jpcap.getDeviceDescription();
atp.appendText("Start capturing on "+lists[0]);

Jpcap jpcap=Jpcap.openDevice(Jpcap.getDeviceList()[0],1000,false,20);
jpcap.loopPacket(-1,new Tcpdump());



// Add some text every second
Timer t = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String timeString = fmt.format(new Date());
atp.appendText(timeString + "\n");
}

SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
});
t.start();
}
}


noticed the main method ive added a few lines from my original file to it and the application now pops up and it says Start capturing on NDIS 5.0 driver but the actual output still goes to the command line and not the gui - i know it probably has something to do with the the code below although i havent moved ths to the new gui code

class Tcpdump implements JpcapHandler
{
public void handlePacket(Packet packet){
System.out.println(packet);
}


Any last tips would be great although youve done more than enough already to help.

Many thanks
 
I think you have two possibilities :

1. Pass the AppendingTextPane "atp" to the constructor of "Tcpdump" and do an "atp.appendText(packet.toString() + "\n");


2. Or let the AppendingTextPane implement JpcapHandler and add the method
Code:
public void handlePacket(Packet packet) {
  atp.appendText(packet.toString()  + "\n");
}
 
Heres my new code now

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.Date;
import jpcap.*;

public class AppendingTextPane extends JTextPane implements JpcapHandler {
public AppendingTextPane() {
super();
}

public AppendingTextPane(StyledDocument doc) {
super(doc);
}

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

// Appends text to the document and ensure that it is visible
public void appendText(String text) {
try {
Document doc = getDocument();

// Move the insertion point to the end
setCaretPosition(doc.getLength());

// Insert the text
replaceSelection(text);

// Convert the new end location
// to view co-ordinates
Rectangle r = modelToView(doc.getLength());

// Finally, scroll so that the new text is visible
if (r != null) {
scrollRectToVisible(r);
}
} catch (BadLocationException e) {
System.out.println("Failed to append text: " + e);
}
}


// Testing method
public static void main(String[] args)throws java.io.IOException {
JFrame f = new JFrame("Text Pane with Scrolling Append");
final AppendingTextPane atp = new AppendingTextPane();
f.getContentPane().add(new JScrollPane(atp));
f.setSize(200, 200);
f.setVisible(true);


String[] lists=Jpcap.getDeviceDescription();
atp.appendText("Start capturing on "+lists[0]);

Jpcap jpcap=Jpcap.openDevice(Jpcap.getDeviceList()[0],1000,false,20);
jpcap.loopPacket(-1,new Tcpdump());



// Add some text every second
Timer t = new Timer(1000, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String timeString = fmt.format(new Date());
atp.appendText(timeString + "\n");
}

SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss");
});
t.start();
}
}


Unfortunatley it comes back with


C:\Project>javac AppendingTextPane.java
AppendingTextPane.java:19: cannot resolve symbol
symbol : variable atp
location: class AppendingTextPane
atp.appendText(packet.toString() + "\n");
^
1 error

I have not tried the other method as i am unsure as how to get tcpdump to read atp as a constructor.

Any ideas? Again thankyou.
 
Sorry, my fault. Try the following : Change

=======
public void handlePacket(Packet packet) {
atp.appendText(packet.toString() + "\n");
}
=======

in

=======
public void handlePacket(Packet packet) {
this.appendText(packet.toString() + "\n");
}
=======

And change
jpcap.loopPacket(-1,new Tcpdump());
in
jpcap.loopPacket(-1,atp);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top