The Artima Developer Community
Sponsored Link

Java Answers Forum
How to implement a JTextArea that shows the messages coming in the console?

14 replies on 1 page. Most recent reply: Nov 16, 2008 9:32 PM by William Robertson

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 14 replies on 1 page
patuco s.

Posts: 12
Nickname: patuco
Registered: Feb, 2006

How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 17, 2006 3:14 AM
Reply to this message Reply
Advertisement
Hi everyone,

Here is my problem: i have a c++ dll created which uses on it several times the function println. Its calls a fortran dll which uses the command write (similar to println in c, to show a message in the console) ok? this library is called with a java program (java is used a s the interface). One of the elementes of this java interface has to show all teh messages tha come in the console, which are the ones by using the funciont printf and write. If you run a program in java (no mather if console, eclipse,...) all this messages are shown, but not in the GUI. So as you can imagine, the purpose is to create a TextArea is to show this messages, but i do not know how to call the console, or what it does to show the messages. In other words, I am trying to use for example System.out and have it come up in the textarea.

Any idea how i can do it? Thanks


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 18, 2006 7:13 PM
Reply to this message Reply
Here is how you do it.

1. Create a class that inherits from java.io.FilterOutputStream
2. Override all write() methods of this class.
3. Instantiate this class and use its instance in System.setOut(<<your class instance>>)
4. Now your class will get all the output inseatd of console.
5. Do whatever you want to do with that output.

Here is an example, which you can use for application wide usage.
1. ConsoleListener Interface: This interface needs to be implemented in a class that wants to be notifed when a message is logged to console
2. Console class: On its fisrt usage in static block, it installs itself as the console. That is, it will intercept all console message
3. You register your class who is interested in console message using
Console.registerOutputListener(<<your listener class instance>>);

4. In your logMessage() method of your listener class, you will receive the console output and you can display it in a text area.
5. ConsoleTest class: This is an example on how to use it.
6. I have used it with System.out.println() Java method. Hopefully, it will work with your dll printf() call too.
You are free to modify the code to suite your requirement.

package kutility;
 
public interface ConsoleListener {
  void logMessage(String message);
}
 
----------------------------------------------------
 
package kutility;
 
import java.io.FilterOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
 
public class Console
    extends FilterOutputStream {
  private static ArrayList registeredListeners = new ArrayList();
 
  static {
    // On its first use. Install the Console Listener
    PrintStream printStream =
        new PrintStream(
            new Console(new ByteArrayOutputStream())
        );
    System.setOut(printStream);
  }
 
  public Console(OutputStream out) {
    super(out);
  }
 
  /* Override Ancestor method */
  public void write(byte b[]) throws IOException {
    String str = new String(b);
    logMessage(str);
  }
 
  /* Override Ancestor method */
  public void write(byte b[], int off, int len) throws IOException {
    String str = new String(b, off, len);
    logMessage(str);
  }
 
  /* Override Ancestor method */
  public void write(int b) throws IOException {
    String str = new String(new char[] { (char) b});
    logMessage(str);
  }
 
  public static void registerOutputListener(ConsoleListener listener) {
    // we don't register null listeners
    if (listener != null) {
      registeredListeners.add(listener);
    }
  }
 
  public static void removeOutputListener(ConsoleListener listener) {
    if (listener != null) {
      registeredListeners.remove(listener);
    }
  }
 
  private static void logMessage(String message) {
    // Log output to each listener
    int count = registeredListeners.size();
    for (int i = 0; i < count; i++) {
      ( (ConsoleListener) registeredListeners.get(i)).logMessage(message);
    }
  }
}
 
----------------------------------------------------
 
package kutility;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
public class ConsoleTest
    extends JFrame implements ConsoleListener {
  private JScrollPane jScrollPane1 = new JScrollPane();
  private JTextArea jTextArea1 = new JTextArea();
  private JButton msgButton = new JButton("Log Message");
 
  public ConsoleTest() {
    try {
      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
 
  private void jbInit() throws Exception {
    this.getContentPane().setLayout(new FlowLayout());
    this.getContentPane().add(jScrollPane1);
    jTextArea1.setRows(10);
    jTextArea1.setColumns(20);
    jTextArea1.setText("");
    this.getContentPane().add(jScrollPane1);
    jScrollPane1.getViewport().add(jTextArea1);
    this.getContentPane().add(this.msgButton);
 
    this.msgButton.addActionListener(
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("Current Date and Tiem is:");
        System.out.println(new java.util.Date());
      }
    }
    );
 
    // Registe this frame as Console Listener
    Console.registerOutputListener(this);
  }
 
  public static void main(String[] args) {
    ConsoleTest consoletest = new ConsoleTest();
    consoletest.setBounds(10, 10, 300, 300);
    consoletest.setVisible(true);
 
  }
 
// Implement the ConsoleListener interface method to log message to Text Area
  public void logMessage(String message) {
    this.jTextArea1.append(message);
  }
 
  public void msgButton_actionPerformed(ActionEvent e) {
    // Log cuttent date and time
    System.out.println(new java.util.Date());
  }
 
}
 

patuco s.

Posts: 12
Nickname: patuco
Registered: Feb, 2006

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 19, 2006 5:17 AM
Reply to this message Reply
Thanks a lot for the answer,
i will try your idea,
Just a question is ti possible to do the same but for the read command, i will explain mysefl. the first problem was to create the text area to show all the messages coming out on a for example windows command console or linux terminal when executing a java or c programm.(That is your code which i´m gonna try in a sec)
Now the point is to know how to use this text area to send messages like the console/terminal such as ´javac -jni c´ or ´cd prjects/java´ and that it answer (if it were possilbe but i think it is)
Thanks

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 19, 2006 8:40 AM
Reply to this message Reply
Look at java.lang.Runtime class and its exec() method. It lets you do what you are looking for. You will be able to execute an OS command which you have as a string in text area.

patuco s.

Posts: 12
Nickname: patuco
Registered: Feb, 2006

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 20, 2006 2:52 AM
Reply to this message Reply
Tnaks a lot Kishori,
I am right now trying your ideas and just a question, how can be done that the area shows the messages automatically instead of clicking the button? i gonna try with the c and i let you know.

patuco s.

Posts: 12
Nickname: patuco
Registered: Feb, 2006

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 20, 2006 3:20 AM
Reply to this message Reply
Hi,
i have tryed your idea and it works but only with the system.out messages not with the printf from the c library. Do you have any idea how can they be showed? Or just how can i emulate the javaw.exe which is the one which is showing the messages in the terminal?
Thanks

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 20, 2006 5:09 PM
Reply to this message Reply
I did try it with dll printf and it doesn't work. I searched on google and found out that there is no good solution for this. suggested solution was to redirect the stdout in your dll to a file and in your java app read the file. Or, changing the call to printf to call a Java method using string from dll after converting printf to s string using sprintf and then using System.out.println. However, this is only possible if you own the dll source code.

Someone who has done it may help you with a good solution.

Thanks
Kishori

patuco s.

Posts: 12
Nickname: patuco
Registered: Feb, 2006

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 21, 2006 3:41 AM
Reply to this message Reply
After performing some tests, it is so:
When the java.output is redirected only the messages coming out from java are shown. It does not receive the c r fortran messages. Anyone know how can be done in order to receive these (printf/write) messages?

Benoit Pesty

Posts: 4
Nickname: tchule
Registered: Aug, 2003

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 21, 2006 5:37 AM
Reply to this message Reply
Hello,

Remembers me a problem trying to launch a Fortran program.

There is a good article on Javaworld on how to capture the output :
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Hope this helps.

Tchule.

patuco s.

Posts: 12
Nickname: patuco
Registered: Feb, 2006

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 21, 2006 7:14 AM
Reply to this message Reply
ould u tell me about your problem because after reading the code i did not find any relations

Benoit Pesty

Posts: 4
Nickname: tchule
Registered: Aug, 2003

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 23, 2006 6:52 AM
Reply to this message Reply
My problem was that the Fortran progam was filling the System.err output and waiting for the system to empty it.
When launched from a CMD shell it was OK, when launched from a java Runtime.Exec it was blocking.

The problem is not the same but you can use the "Gobbler" solution to read the output from the System.out (and System.err) and update your JTextArea.

Tchule.

patuco s.

Posts: 12
Nickname: patuco
Registered: Feb, 2006

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 23, 2006 10:45 AM
Reply to this message Reply
Thanks a lot,
could you explain me how to use the "Gobbler"? or how you did to empty the console?? because it could be as you said that the console is not "flusing" the messages.

all what i have tryed does not work and i am really desperado. So please it would be great if you explain me how you solve the problem.

Here is what i have tryed, everything works with the system.out but not with the printf (maybe because the cmd is blocked??????) :
1.-
<code>
public static void main(String argh_my_aching_fingers[]){
//-----------------------------------------------------
final JTextArea textArea = new JTextArea(15, 40);
// Get the default system.out

final PrintStream sysOut = new PrintStream(System.out);

System.setOut(new PrintStream(new OutputStream() {
public void write(int b) throws IOException {
// write to the text area
textArea.append(String.valueOf((char)b));
// write to the default System.out
sysOut.write(b);
}
}));
}


2.-
public static void main(String argh_my_aching_fingers[]){
try
{
// create a buffered reader that connects to the console, we use it so we can read lines
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

// read a line from the console
String lineFromInput = in.readLine();

// create an print writer for writing to a file
PrintWriter out = new PrintWriter(new FileWriter("myoutput.txt"));

// output to the file a line
out.println(lineFromInput);

// close the file (VERY IMPORTANT!)
out.close();
}
catch(IOException e)
{
System.out.println("Error during reading/writing");
}

}

the file nly received the messages from the system.out


3.- the same, at least to write into a file everything but again failed.
<code>
// escribir en un archivo no funciona
try {
// Tee standard output
PrintStream out = new PrintStream(new FileOutputStream("out.log"));
PrintStream tee = new TeeStream(System.out, out);

System.setOut(tee);

// Tee standard error
PrintStream err = new PrintStream(new FileOutputStream("err.log"));
tee = new TeeStream(System.err, err);

System.setErr(tee);
} catch (FileNotFoundException e) {
}

// Write to standard output and error and the log files
System.out.println("welcome");

MainLogical logicalJava = new MainLogical();
logicalJava.GetIECRI1();// .GetInformationCommands();
System.out.println("bye");
<endcode>

NOTE / HINT:
that is the interface of the console:

<code>
//Make sure a nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
JFrame frame = new JFrame("Ls test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(textArea,BorderLayout.CENTER);
frame.setSize(100,100);//setExtendedState(MAXIMIZED_BOTH);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
//-----------------------------------------------------

<endcode>

patuco s.

Posts: 12
Nickname: patuco
Registered: Feb, 2006

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Feb 25, 2006 3:58 AM
Reply to this message Reply
Hi Tchule,
i think i have the same problem, when i execute the program with the debugger the messages come from the library, but when it ends a couple of new messages come from the system, it could be the same as you described. The point is how can i do to solve it??
Many many thanks
Louis

Samuel Picture

Posts: 2
Nickname: spicture
Registered: Nov, 2008

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Nov 10, 2008 12:28 PM
Reply to this message Reply
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>

William Robertson

Posts: 6
Nickname: billr42
Registered: May, 2008

Re: How to implement a JTextArea that shows the messages coming in the console? Posted: Nov 16, 2008 9:32 PM
Reply to this message Reply
The code declares frame as a JFrame.

JFrame frame;

and then it tries to to call that method on frame.

frame.registerOutputListener(this);

If you look at the javadoc for frame, http://java.sun.com/javase/6/docs/api/javax/swing/JFrame.html you will see that registerOutputListener does not exist on JFrame. So that is the source of the error.

I don't see any classes in the standard runtime libraries that are called ConsoleListener, and I don't really see anything in the jpcap api documentation for that either. So its not clear what it is that you're trying to do here, but this might be what you're looking for...

First implement PacketReceiver instead of ConsoleListener e.g.
public class JpcapCapturePacket extends JFrame implements PacketReceiver {
...

then implement receivePacket in your JpcapCapturePacket class. e.g.

public void receivePacket(Packet packet) {
textArea.append(packet);
}

And then when you call loopPacket, pass in this as the thing that receives the packets.

JpcapCaptor captor = JpcapCaptor.openDevice(devices[interfaceNo], 65535, false, 20);
captor.loopPacket(-1, this);

You are fortunate here, because JTextArea.append is one of the few threadsafe methods in the swing API.

Good luck!

Flat View: This topic has 14 replies on 1 page
Topic: helpppp Previous Topic   Next Topic Topic: Method help

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use