|
|
Re: How to implement a JTextArea that shows the messages coming in the console?
|
Posted: Nov 10, 2008 12:28 PM
|
|
I am a retired mainframe basic assembly language programmer trying to teach himself Java. I developed a Java Swing application using Jpcap to capture paackets for a specific local computer network interface, which is specified through a program selection dialog. I want the application to use it's JTextArea for System.out.println (console) messages. I used the Console.java and ConsoleListener.java file from this article in conjunction with my jcapCapturePackets.java file. My program will not compile without a "cannot find symbol method registerOutputListener(JpcapCapturePacket)". I have looked, and looked but I cannot determine why I am getting the error i.e. I am lost.
I have included the source code JpcapCapturePackets.java. I will sincerely apprepciate some help in resolving the error.
<code> import java.*; import java.net.*; import java.text.*;
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.*;
import jpcap.*; import jpcap.JpcapCaptor; import jpcap.NetworkInterface; import jpcap.packet.Packet;
public class JpcapCapturePacket extends JFrame implements ConsoleListener { JFrame frame; JTextArea textArea; JpcapCaptor captor; JpcapCapturePacket jpcapCapturePacket; int duration; int interfaceNo; static URL url1 = JpcapCapturePacket.class.getResource("images/Start.gif"); ImageIcon start = new ImageIcon(url1); static URL url2 = JpcapCapturePacket.class.getResource("images/Stop.gif"); ImageIcon stop = new ImageIcon(url2); static URL url3 = JpcapCapturePacket.class.getResource("images/Clear.gif"); static ImageIcon clear = new ImageIcon(url3); static URL url4 = JpcapCapturePacket.class.getResource("images/Print.gif"); static ImageIcon print = new ImageIcon(url4); static URL url5 = JpcapCapturePacket.class.getResource("images/Exit.gif"); static ImageIcon exit = new ImageIcon(url5); static URL url6 = JpcapCapturePacket.class.getResource("images/About.gif"); static ImageIcon about = new ImageIcon(url6); static URL url7 = JpcapCapturePacket.class.getResource("images/Logo_Small.gif"); static ImageIcon logoSmall = new ImageIcon(url7); static URL url8 = JpcapCapturePacket.class.getResource("images/Logo_Large.gif"); static ImageIcon logoLarge = new ImageIcon(url8); static URL url9 = JpcapCapturePacket.class.getResource("images/Splash_Scrn.jpg"); static ImageIcon splashScrn = new ImageIcon(url9); public JpcapCapturePacket() { super("Jpcap Packet Capture Version 1.00"); // // Register the application's frame as a Console listener so that we can // redirect Console messages to the it's frame. // frame.registerOutputListener(this);
ShowSplashScreen(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(JpcapCapturePacket mw) { super("File"); jpcapCapturePacket = 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(JpcapCapturePacket mw) { super("Help"); jpcapCapturePacket = 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 + " " + (devices.description == null?devices.name:devices.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) { System.out.println(packet); } }
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 = JpcapCapturePacket.class.getResource(path); if (imgURL != null) { return new ImageIcon(imgURL); } else { System.err.println("Couldn't find file: " + path); return null; } } public void ShowSplashScreen(int d) { duration = d; JPanel splashScreen = (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())); splashScreen.add(label, BorderLayout.CENTER); setVisible(true);
try { Thread.sleep(duration); } catch (Exception e) { }
splashScreen.remove(label); setVisible(false); } public static void main(String[] args) { JpcapCapturePacket JpcapCapturePacket = new JpcapCapturePacket(); JFrame.setDefaultLookAndFeelDecorated(true); JpcapCapturePacket.setIconImage(logoSmall.getImage()); JpcapCapturePacket.setSize(600, 360); Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); int w = JpcapCapturePacket.getSize().width; int h = JpcapCapturePacket.getSize().height; int x = (dim.width - w) / 2; int y = (dim.height - h) / 2; JpcapCapturePacket.setLocation(x, y); JpcapCapturePacket.setVisible(true); } // // Implement the ConsoleListener interface method to log console messages to the // application's Text Area. // public void logMessage(String message) { this.textArea.append(message); } } </code>
|
|