Connecting GUI & Application


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Page 1 of 2 12 LastLast
Results 1 to 15 of 28

Thread: Connecting GUI & Application

Hybrid View

  1. #1
    Join Date
    Jan 2008
    Posts
    29

    Connecting GUI & Application

    hey guys, i'm having a little problem!
    I have been able to write a little application on packet sniffing wiz HELP of coarse ! now i want to add a GUI to it! i tried but it won't work......anyone who can help.....PLEASE.....

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Welcome to DevX

    What have you tried that isn't working?

    Why isn't it working? Are you getting errors? If so, what are they?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Jan 2008
    Posts
    29

    Problem & Errors

    I just learnt java.awt and still learning...
    I have been able to create a simple frame with action events..

    here a sample code
    Code:
    public boolean action(Event e, Object o) {
    			if ("Start".equals (o))
    				try {
    					new packets(); // external class && BIG PROBLEMS
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    			else if ("Save".equals (o))
    			{
    				System.out.println ("Save Me");
    				
    			}	
    			else if ("Quit".equals (o))
    				System.exit (0);
    			hide();
    			return true;
    		}
    
    and here are the errors:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
    	at packets.<init>(packets.java:50)
    	at PopupWindow.action(capture_packet_GUI.java:73)
    	at java.awt.Component.handleEvent(Unknown Source)
    	at java.awt.Window.postEvent(Unknown Source)
    	at java.awt.Component.postEvent(Unknown Source)
    	at java.awt.Component.postEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    If anyone can help..please..!

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The error message appears related to the "new packets()" statement in your try block. How are you supposed to be constructing a Packets object?

  5. #5
    Join Date
    Jan 2008
    Posts
    29

    Heeeeeeeeeeeeeeeeeeeeeeeeeeelp

    The packets class works well! i have been able to capture incoming packets.
    I just want to place that in a GUI.


    Here the new complete code of the GUI:

    import java.awt.BorderLayout;
    import java.awt.Insets;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.IOException;

    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.SwingUtilities;


    public class capture_packet_GUI extends JPanel implements ActionListener{
    static private String newline = "\n";
    JButton startButton;
    JTextArea log;

    public capture_packet_GUI(){
    super(new BorderLayout());
    log = new JTextArea(10,80); // set size of frame
    log.setMargin(new Insets(5,5,5,5));
    log.setEditable(false);
    JScrollPane logScrollPane = new JScrollPane(log);


    startButton = new JButton("Start Capture");
    startButton.addActionListener(this);

    // another frame for the buttons
    JPanel buttonPanel = new JPanel();
    buttonPanel.add(startButton);

    add(buttonPanel,BorderLayout.PAGE_START);
    add(logScrollPane,BorderLayout.CENTER);

    }

    public void actionPerformed(ActionEvent e){
    if(e.getSource() == startButton){
    try{
    display_packets();
    log.append("HAHAHAH working" + newline);
    }catch(IOException ioe){
    System.out.println("ERROR"+ioe);
    }

    }
    else{
    log.append("Cancel" + newline);

    }
    log.setCaretPosition(log.getDocument().getLength());
    }

    // a new instance of packets
    public void display_packets() throws IOException{
    packets p1 = new packets();
    System.out.println(p1);

    }


    public static void createAndShowGUI(){
    JFrame frame = new JFrame("FILE CHOOSER 2");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new capture_packet_GUI());
    frame.pack();
    frame.setVisible(true);

    }

    public static void main(String args[]){
    createAndShowGUI();

    }


    }
    Main Goal :-
    I want to print the capture packets in the TextArea [log = new JTextArea(10,80);]!!!

  6. #6
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    It might be one of the field / properties in packet class that is initialized outside the constructor. The ArrayOutOfBounds Exception occurs only when an array is accessed that is not initialized or you are accessing a non existent location. Double check that. Maybe you are using some static method / initializer to create the array, and it is not being called.

    Include the code for packets.java. It doesn't hurt to check that too. Does it?

  7. #7
    Join Date
    Jan 2008
    Posts
    29

    packets.java class

    Hmmmm no array has been used so far....(as far as i remeber though) but here you are the packets.java class and packetprinter!!!
    please if you just could check that for me PLEASE!!!!
    Code:
    packets.java
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    import jpcap.*;
    import jpcap.packet.Packet;
    
    public class packets {
    	
    	// CONSTRUCTOR
    	packets() throws IOException{
    		//		Obtain the list of network interfaces
    		NetworkInterface[] devices = JpcapCaptor.getDeviceList();
    		
    		//for each network interface
    		for (int i = 0; i < devices.length; i++) {
    			//print out its name and description
    			System.out.println(i+": "+devices[i].name + "(" + devices[i].description+")");
    			//print out its datalink name and description
    			System.out.println(" datalink: "+devices[i].datalink_name + "(" + devices[i].datalink_description+")");
    		
    			//print out its MAC address
    			System.out.print(" MAC address:");
    		
    			for (byte b : devices[i].mac_address)
    				 System.out.print(Integer.toHexString(b&0xff) + ":");
    				 System.out.println();
    
    				 //print out its IP address, subnet mask and broadcast address		
    			for (NetworkInterfaceAddress a : devices[i].addresses)
    				 System.out.println(" address:"+a.address + " " + a.subnet + " "+ a.broadcast);
    		}	// end for	  
    		
    		
    			/*
    		BufferedReader bt = new BufferedReader (new InputStreamReader (System.in));
    		int index;
    		String str;
    		System.out.println ("Enter device number");
    		str = bt.readLine();
    		index = Integer.parseInt(str);
    			*/
    		int index = 1;
    		
    		//Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms)
    		JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 20);
    		//captor.setFilter("port 21", true);
    		captor.loopPacket(-1, new PacketPrinter());
    		
    		//captor.processPacket(-1,new PacketPrinter());
    		captor.close();
    
    		
    	}
    		
    }
    And PacketPrinter.java
    Code:
    import jpcap.PacketReceiver;
    import jpcap.packet.Packet;
    
    public class PacketPrinter implements PacketReceiver{
    
    	public void receivePacket(Packet packet) {
    		System.out.println(packet);
    		
    	}
    
    }
    try this out and see if you can just make the packet class and the capture_packets_GUI class coexist!!!!
    Last edited by Hack; 01-14-2008 at 02:54 PM. Reason: Added Code Tags

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The first thing this constructor does is to create an array of network interfaces through a call to JcapCaptor.

    What statement is line 50 of the packets.java code?

  9. #9
    Join Date
    Jan 2008
    Posts
    29

    Line 50

    "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1"
    Actually what does that mean????

    The line 50 opens an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms)

    JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 20);

    what's wrong with this line????

  10. #10
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You have hard-coded index to equal 1. If there are not at least two devices, your attempt to call devices[1] will be out of bounds

  11. #11
    Join Date
    Jan 2008
    Posts
    29
    hmmm that's not the problem, there are 2 devices
    - modem
    - ADSL modem

    Both have been detected...

    i think the problem lies in the creation of a new instance of packets when the method display_packets() is called. Have you been able to try the code???
    i'm using eclipse.

  12. #12
    Join Date
    Jan 2008
    Posts
    1
    Hi Friends

    Here the new and complete information about Connecting Gui Application

    http://quick-help.net/connecting-gui--application

  13. #13
    Join Date
    Jan 2008
    Posts
    29
    elo guys, i was able to enter the captured packets in a table. just got a little problem....the table rows is not updated each time a row is inserted that is when a packet is captured..

  14. #14
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Do you refresh the table?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  15. #15
    Join Date
    Jan 2008
    Posts
    29
    well how i do refresh the table.!!
    i have tried several methods (eg fireTableDataChanged() ) but truely i dont know how to use the methods in the AbstractTableModel class!!

Similar Threads

  1. Replies: 0
    Last Post: 06-15-2005, 08:31 PM
  2. One Application to Control Many
    By mythol in forum .NET
    Replies: 1
    Last Post: 05-19-2005, 06:14 PM
  3. Replies: 13
    Last Post: 10-23-2001, 02:37 PM
  4. Replies: 1
    Last Post: 04-14-2000, 11:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center