How to display data to Jtable. I had got the following code to display jpcap packet captured data to JTable. But it didn't worked.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import net.sourceforge.jpcap.capture.PacketCapture;
import net.sourceforge.jpcap.capture.PacketListener;
import net.sourceforge.jpcap.net.ARPPacket;
import net.sourceforge.jpcap.net.ICMPPacket;
import net.sourceforge.jpcap.net.IPPacket;
import net.sourceforge.jpcap.net.Packet;
import net.sourceforge.jpcap.net.TCPPacket;
import net.sourceforge.jpcap.net.UDPPacket;
public class NetworkStatistics implements PacketListener{
public JFrame frmNmcidsNetworkStatistics;
private Vector<String> row;
private Vector<Vector> rowData;
private Vector<String> columnNames;
private DefaultTableModel DFMO;
private JTable table;
private JScrollPane scrollPane;
private PacketCapture captureobj;
private String filter = "";
private int totalpackets = -1;
private static int counter = 0;
/*
* Create the application.
*/
public NetworkStatistics() {
initialize();
}
/*
* Initialize the contents of the frame.
*/
private void initialize() {
frmNmcidsNetworkStatistics = new JFrame();
frmNmcidsNetworkStatistics.setTitle("NMCIDS: Network Statistics");
frmNmcidsNetworkStatistics.setBounds(100, 100, 450, 300);
frmNmcidsNetworkStatistics.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
row = new Vector<String>();
rowData = new Vector<Vector>();
columnNames = new Vector<String>();
columnNames.addElement("Sr. No");
columnNames.addElement("Source IP");
columnNames.addElement("Source MAC");
columnNames.addElement("Destination IP");
columnNames.addElement("Destination MAC");
columnNames.addElement("Protocol");
DFMO = new DefaultTableModel(rowData, columnNames);
table = new JTable(DFMO);
scrollPane = new JScrollPane(table);
frmNmcidsNetworkStatistics.getContentPane().add(scrollPane, BorderLayout.NORTH);
}
public void GetNetworkPackets(String capdev){
try{
captureobj = new PacketCapture();
captureobj.open(capdev, true);
captureobj.setFilter(filter, true);
captureobj.addPacketListener(this);
captureobj.capture(totalpackets);
}
catch(Exception e){
e.printStackTrace();
}
}
public void dispalyPacketsTable(int srno, String sip, String smac, String dip, String dmac, String proto){
row.addElement(""+srno);
row.addElement(sip);
row.addElement(smac);
row.addElement(dip);
row.addElement(dmac);
row.addElement(proto);
rowData.addElement(row);
DFMO.fireTableDataChanged();
}
@Override
public void packetArrived(Packet pdata) {
counter++;
printdata(pdata);
}
public void printdata(Packet pdata){
if(pdata instanceof TCPPacket){
TCPPacket TCPO = (TCPPacket) pdata;
dispalyPacketsTable(counter, TCPO.getSourceAddress(), TCPO.getSourceHwAddress(), TCPO.getDestinationAddress(), TCPO.getDestinationHwAddress(), "TCP");
}
}
}
The program hangs when i run this code. But when i want to display the packet captured data to command promt it displays correctly. Please help me out of this. Thank you in advance.
The program hangs when i run this code
Probably because your code is running on the Event Dispatch Thread(EDT)
which prevents the GUI from responding to events.
Your packet listener needs to run on a separate Thread so you don't block the EDT. Read the section from the Swing tutorial on Concurrency in Swing. I would guess you should be using a SwingWorker
and the you can publish
the results as they become available.