如何向Jtable顯示數據。我得到了下面的代碼來顯示JTable的jpcap數據包捕獲的數據。但它沒有奏效。如何將jpcap數據包捕獲的數據顯示給JTable?
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");
}
}
}
當我運行此代碼時程序掛起。但是,當我想顯示數據包捕獲的數據命令promt它顯示正確。請幫我解決這個問題。先謝謝你。
變量名稱不應以大寫字符開頭。爲什麼「DFMO」不同?始終如一!!! – camickr