Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Jpcap captured packets not appearing in Swing application's text area
Sam Picture
Greenhorn
Posts: 2
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Can anyone help me understand why the Jpcap captured packets are not appearing in the following Swing application's text area?
import java.awt.*; import java.awt.event.*; import java.text.*; import java.net.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import jpcap.*; import jpcap.JpcapCaptor; import jpcap.NetworkInterface; import jpcap.packet.Packet; public class JpcapPacketCapture extends JFrame { JFrameframe; JProgressBarprogressBar; JTextAreatextArea; JpcapCaptorcaptor; JpcapPacketCapture packetCapture; int duration; int interfaceNo; static URL url1 = JpcapPacketCapture.class.getResource("images/Start.gif"); ImageIcon start = new ImageIcon(url1); static URL url2 = JpcapPacketCapture.class.getResource("images/Stop.gif"); ImageIcon stop = new ImageIcon(url2); static URL url3 = JpcapPacketCapture.class.getResource("images/Clear.gif"); static ImageIcon clear = new ImageIcon(url3); static URL url4 = JpcapPacketCapture.class.getResource("images/Print.gif"); static ImageIcon print = new ImageIcon(url4); static URL url5 = JpcapPacketCapture.class.getResource("images/Exit.gif"); static ImageIcon exit = new ImageIcon(url5); static URL url6 = JpcapPacketCapture.class.getResource("images/About.gif"); static ImageIcon about = new ImageIcon(url6); static URL url7 = JpcapPacketCapture.class.getResource("images/Logo_Small.gif"); static ImageIcon logoSmall = new ImageIcon(url7); static URL url8 = JpcapPacketCapture.class.getResource("images/Logo_Large.gif"); static ImageIcon logoLarge = new ImageIcon(url8); static URL url9 = JpcapPacketCapture.class.getResource("images/Splash_Scrn.jpg"); static ImageIcon splashScrn = new ImageIcon(url9); public JpcapPacketCapture() { super("Jpcap Packet Capture Version 1.00"); showSplash(4000); FileMenu fileMenu = new FileMenu(this); HelpMenu helpMenu = new HelpMenu(this); MenuBar menuBar = new MenuBar(); menuBar.add(fileMenu); menuBar.add(helpMenu); setMenuBar(menuBar); JToolBar toolBar = new JToolBar(); toolBar.setFloatable(true); toolBar.setOrientation(JToolBar.HORIZONTAL); toolBar.setRollover(true); addButtons(toolBar); textArea = new JTextArea(14, 42); textArea.setTabSize(4); textArea.setFont(new Font("sansserif", Font.PLAIN, 12)); textArea.setCaretPosition(textArea.getDocument().getLength()); textArea.setEditable(false); Container contentPane = getContentPane(); JPanel panel = new JPanel(); contentPane.add(new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS)); contentPane.add(toolBar, "North"); } class FileMenu extends Menu { public FileMenu(JpcapPacketCapture mw) { super("File"); packetCapture = mw; MenuItem mi; add(mi = new MenuItem("Start Capure")); mi.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { try { StartCapture(); } catch(Exception evt) { evt.printStackTrace(); } } } ); add(mi = new MenuItem("Stop Capure")); mi.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { try { StopCapture(); } catch(Exception evt) { evt.printStackTrace(); } } } ); add(mi = new MenuItem("Clear Window")); mi.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { textArea.setText(""); } } ); add(mi = new MenuItem("Print Window")); mi.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { PrintWindow(); } } ); add(mi = new MenuItem("Exit")); mi.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } } ); } } class HelpMenu extends Menu { public HelpMenu(JpcapPacketCapture mw) { super("Help"); packetCapture = mw; MenuItem mi; add(mi = new MenuItem("About...")); mi.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent evt) { JOptionPane.showMessageDialog(frame, "Jpcap Packet Capture Version 1.00 \n\n" + "This progoram uses Jpcap to capture packets for one of this system's \n" + "network interfaces. The specific network interface to capture packets \n" + "for is provided by the user through a program selection dialog. \n\n", "About...", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(logoLarge.getImage())); } } ); } } public void addButtons(JToolBar toolBar) { JButton toolbarButtons = null; toolbarButtons = new JButton(new ImageIcon(start.getImage())); toolbarButtons.setToolTipText("Start capture."); toolbarButtons.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { try { StartCapture(); } catch(Exception evt) { evt.printStackTrace(); } } } ); toolBar.add(toolbarButtons); toolbarButtons = new JButton(new ImageIcon(stop.getImage())); toolbarButtons.setToolTipText("Stop capture."); toolbarButtons.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { try { StopCapture(); } catch(Exception evt) { evt.printStackTrace(); } } } ); toolBar.add(toolbarButtons); toolbarButtons = new JButton(new ImageIcon(clear.getImage())); toolbarButtons.setToolTipText("Clear window."); toolbarButtons.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { textArea.setText(""); } } ); toolBar.add(toolbarButtons); toolbarButtons = new JButton(new ImageIcon(print.getImage())); toolbarButtons.setToolTipText("Print window."); toolbarButtons.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { PrintWindow(); } } ); toolBar.add(toolbarButtons); toolbarButtons = new JButton(new ImageIcon(exit.getImage())); toolbarButtons.setToolTipText("Exit program."); toolbarButtons.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } } ); toolBar.add(toolbarButtons); toolbarButtons = new JButton(new ImageIcon(about.getImage())); toolbarButtons.setToolTipText("About program."); toolbarButtons.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame, "Jpcap Packet Capture Version 1.00 \n\n" + "This progoram uses Jpcap to capture packets for one of this system's \n" + "network interfaces. The specific network interface to capture packets \n" + "for is provided by the user through a program selection dialog. \n\n", "About...", JOptionPane.INFORMATION_MESSAGE, new ImageIcon(logoLarge.getImage())); } } ); toolBar.add(toolbarButtons); } public void StartCapture() { NetworkInterface[] devices = JpcapCaptor.getDeviceList(); String[] names = new String[devices.length]; for (int i = 0; i < names.length; i++) names[i] = i + " " + (devices[i].description == null?devices[i].name :mrgreen: evices[i].description); Object[] selectionValues = names; String initialSelection = names[0]; Object selection = JOptionPane.showInputDialog(null, "Capture using which interface?\n", "Input", JOptionPane.PLAIN_MESSAGE, null, selectionValues, initialSelection); textArea.append(selection + "\n"); textArea.select(0, 1); interfaceNo = Integer.parseInt(textArea.getSelectedText()); textArea.setText(""); try { JpcapCaptor captor = JpcapCaptor.openDevice(devices[interfaceNo], 65535, false, 20); captor.loopPacket(-1,new PacketPrinter()); } catch (java.io.IOException io) { } } class PacketPrinter implements PacketReceiver { public void receivePacket(Packet packet) { str.delete(0, str.length()); str.append(packet + "\n"); textArea.append(str + ""); } } public void StopCapture() { captor.close(); } public void PrintWindow() { final MessageFormat header = new MessageFormat(""); final MessageFormat footer = new MessageFormat(""); textArea.setFont(new Font("sansserif", Font.PLAIN, 9));; try { textArea.print(header, footer, true, null, null, true); } catch(Exception e) { e.printStackTrace(); } textArea.setFont(new Font("sansserif", Font.PLAIN, 12)); } protected static ImageIcon createImageIcon(String path) { java.net.URL imgURL = JpcapPacketCapture.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } public void showSplash(int d) { duration = d; JPanel splash = (JPanel)getContentPane(); int width = 470; int height =255; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int x = (screen.width-width) / 2; int y = (screen.height-height) / 2; setBounds(x, y, width, height); JLabel label = new JLabel(new ImageIcon(splashScrn.getImage())); splash.add(label, BorderLayout.CENTER); setVisible(true); try { Thread.sleep(duration); } catch (Exception e) { } splash.remove(label); setVisible(false); } public static void main(String[] args) { JpcapPacketCapture JpcapPacketCapture = new JpcapPacketCapture(); JFrame.setDefaultLookAndFeelDecorated(true); JpcapPacketCapture.setIconImage(logoSmall.getImage()); JpcapPacketCapture.setSize(600, 360); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); int w = JpcapPacketCapture.getSize().width; int h = JpcapPacketCapture.getSize().height; int x = (dim.width - w) / 2; int y = (dim.height - h) / 2; JpcapPacketCapture.setLocation(x, y); JpcapPacketCapture.setVisible(true); JpcapPacketCapture.addWindowListener ( new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } } ); } }
[Edit: Added code tags MG]
[ November 07, 2008: Message edited by: Maneesh Godbole ]
Maneesh Godbole
Bartender
Posts: 11497
19
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi Sam,
Welcome to the ranch. We are happy to have you here
In future please use the
Code Tags
. I have edited your original post. As you can see the code is now easier to understand because of the indentation.
[
How to ask questions
] [
Donate a pint, save a life!
] [
Onff-turn it on!
]
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Query on ProgressBar(Help plz)
how to control components of another window
How to display toolbars like forte4java
How can I use the Class JpcapCaptor loopPacket method without blocking my Windows app
Adding a GUI to an existing program
More...