Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6043079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:51:37+00:00 2026-05-23T06:51:37+00:00

I am developing some bandwidth utilization monitor tool using jpcap. what am i doing:-

  • 0

I am developing some bandwidth utilization monitor tool using jpcap.
what am i doing:-

1. i created a radio buttons panel containing the list of all the network interfaces that is present on system, and user has to choose one of them.

2. a jfreechart panel (dynamic) that will show the real time graph of bandwidth utilization when user clicks GO! button.

problem:-

i have added following in ActionListener in GO! button

try{captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);}catch(Exception e){}
timer.start();
    captor.loopPacket(-1,new PacketPrinter());

so when i run the program GUI comes with radiobutton panel and jfreechart panel but when i select an option and press GO! application freezes and chart panel does not show any dynamic updation.

when i commented out the JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);
then when i press GO! button, everything works,for example timer starts and chart panel is being updated.(but as captor is null so it is not capturing any data)

Please Help me!!!

my system is ubuntu 10.04, jpcap 0.7

my code is as follows:-

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.DynamicTimeSeriesCollection;
import org.jfree.data.time.Second;
import org.jfree.data.xy.XYDataset;
import java.util.List;
import java.util.ArrayList;
import jpcap.*;
import jpcap.packet.*;
import java.util.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.swing.*;

public class Sniffer extends JPanel
{
    private static jpcap.NetworkInterface[] devices;
    private static int selecteddevice=-1;
    JFrame frame;
    public static JpcapCaptor captor;
    private static final String TITLE="Bandwidth Utilization Meter";;
public static List<Packet> packets;
public static Timer timer;
   public static ArrayList<JRadioButton> radioButtonArray = new ArrayList<JRadioButton>();
private ButtonGroup group= new ButtonGroup();
    public static JButton  go;
    JFreeChart chart;
    static DynamicTimeSeriesCollection dataset;
    public Sniffer()
    {

         packets = new ArrayList<Packet>();
         dataset =new DynamicTimeSeriesCollection(1,120, new Second());
         dataset.setTimeBase(new Second(0, 0, 0, 2, 1, 2011));
         dataset.addSeries(new float[0], 0, "PPP0 Bandwidth Utilization Meter");
         chart = createChart(dataset); 
         getDevices();

        timer = new Timer(1000,new ActionListener(){
            public void actionPerformed(ActionEvent e)

            {    long tlen=0;
                List<Packet> temp = new ArrayList<Packet>(packets);
                packets.clear();
                for(Packet i : temp)
                {
                    tlen+=i.len;
                }
                float[] newData = new float[1];
                newData[0]=(float)tlen/1024;
                dataset.advanceTime();
                dataset.appendData(newData);
                }});
         setGUI();



    }


    void setGUI()
    {
        setLayout(new BorderLayout());
        frame = new JFrame();
        JPanel panel = new JPanel(new GridLayout(devices.length, 1));
        for (JRadioButton combo : radioButtonArray) 
        {
            panel.add(combo);
        }
        JScrollPane scrollPane = new JScrollPane(panel);
        go= new JButton("GO!");
            go.addActionListener(new ActionListener()
                {
                    public void actionPerformed(ActionEvent ee)
                    {
   //problem starts here.............     
                        try{captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);}catch(Exception e){}
                        timer.start();
                        captor.loopPacket(-1,new PacketPrinter());
     //.....................................


                    }
            }
            );

            go.setEnabled(false);    

            panel.add(go);
            add(scrollPane, BorderLayout.CENTER);
            scrollPane.setSize(300,300);
            JFrame.setDefaultLookAndFeelDecorated(true);
            frame.setLayout(new GridLayout(2, 0));
            frame.add(scrollPane);
            frame.add(new ChartPanel(chart));
            frame.setSize(1024, 768);
          frame.setTitle("BW");
          frame.setVisible(true);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void getDevices()
    {
        devices = JpcapCaptor.getDeviceList();
        for(int i=0;i<devices.length;i++)
        {
            String device=null;
            radioButtonArray.add(new JRadioButton());
            group.add(radioButtonArray.get(i));
            radioButtonArray.get(i).addActionListener(new RadioButtonListener());
            device= devices[i].name+" "+"("+devices[i].description+")";
            radioButtonArray.get(i).setText(device);
            }    
    }
    public static void startSniffing() throws Exception
    {
        captor = JpcapCaptor.openDevice(devices[selecteddevice], 65535,true, 20);
    }
    public static void setSelectedDevice(int device) 
    {
        selecteddevice = device;
    }

    public NetworkInterface getSelectedDevice()
    {
        if (selecteddevice == -1) 
        {
            return null;
        } 
        else 
        {
            return devices[selecteddevice];
        }
    }
    public static void main(String args[])
    {
         Sniffer sniffer = new Sniffer();
         //sniffer.start();

    }
     private JFreeChart createChart(final XYDataset dataset) {
    final JFreeChart xyz = ChartFactory.createTimeSeriesChart(
        TITLE, "Time(Seconds)", "Bandwidth KB/s", dataset, true, true, false);
    final XYPlot plot = xyz.getXYPlot();
    ValueAxis domain = plot.getDomainAxis();
    domain.setAutoRange(true);
     ValueAxis range = plot.getRangeAxis();
    range.setRange(0,1000);
    return xyz;
}
}
 class RadioButtonListener extends JPanel implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        Sniffer.go.setEnabled(true);

        for (JRadioButton radio : Sniffer.radioButtonArray) {
            if (radio.isSelected()) {

                Sniffer.setSelectedDevice(Sniffer.radioButtonArray.indexOf(radio));

            }
        }
    }
}


class PacketPrinter implements PacketReceiver {
static long tlen;
public void receivePacket(Packet packet) {
  Sniffer.packets.add(packet);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:51:38+00:00Added an answer on May 23, 2026 at 6:51 am

    Don’t block the EDT. Put the time consuming task in a SwingWorker.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing some program in C# which will send the mail using outlook
I am using Android SDK 1.6 and developing some simple apps. It seems everytime
I am using a custom font on the website i'm developing and some basic
I'm developing some ajax script and using wordpress and my question is: is there
I am developing some client side Javascript that is using some JSON web services
Background We are developing some in-house utilities using ASP.NET 2.0. One of which is
I'm developing some plugins to my application using the JEDI Plugin tecnology, but I
I have a simple program I'm developing to perform some bandwidth tests on remote
I am developing some web services that must be implemented using JAXWS, and that
I am developing some software using VTK. I have compiled VTK succesfully using CMake.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.