How to implement a JTextArea that emulates the console (messages)?

Hi everyone,

here is my problem, for the moment in my opinion enormous.

I am creating an application and one of its parts should be a Text Area whish shows (only read-only) the messages as the console(MSDOs or the one of eclipse, that is the one i磎 using) does.

I am using eclipse and it has a console (as teh commad in MSDOS or the terminal in linux) and all the messages showed there have to be seen in the mentioned TextArea. I do not really know how to implement and how to use if it is neccesary (I really doubt it) the listener.

If anyone could help me, it would be great.

Tanks.

The code done for the moment for this part is reallys obvious:

//Build the menu bar of the main application

public JComponent createConsole() {

//group the buttons in a panel to be together

JPanel panelConsole = new JPanel(new BorderLayout());

//add the text

JTextArea textArea = new JTextArea(5, 10);

Color bg = new Color(0,0,0);

textArea.setBackground(bg);

//Configure the text fonts

textArea.setFont(new Font("Serif", Font.ITALIC, 16));

textArea.setText(

"This is an editable JTextArea " +

"which reacts as the console."

);

//Set the wrap text option

textArea.setLineWrap(true);

textArea.setWrapStyleWord(true);

//add the scrollbars

JScrollPane areaScrollPane = new JScrollPane(textArea);

areaScrollPane.setVerticalScrollBarPolicy(

JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

return panelConsole;

}

Thanks

[1580 byte] By [patucosa] at [2007-10-2 12:29:20]
# 1

a little bit of more explanation. there is a c++ dll created which uses the method println and 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 calles with a java program (java is used a s the interface) so one of the elementes of this interface has to show this println. If you runa 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 of the TextArea is to show this messages, but i do not know how to call the console, or what it does to show the messages.

I have read that in C there is something like AllocConsole, but i do not even know in java, and how to use it

Thanks

patucosa at 2007-7-13 9:25:25 > top of Java-index,Security,Event Handling...
# 2

I will reexplain the problem in order to make it clear:

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

patucosa at 2007-7-13 9:25:25 > top of Java-index,Security,Event Handling...
# 3

>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

Yes :)

public class MyFrameWithConsole extends JFrame {

public MyFrameWithConsole() {

JTextArea consoleTextArea = new JTextArea();

add(consoleTextArea);

System.setOut(new TextAreaPrintStream(consoleTextArea));

}

private static class TextAreaPrintStream extends PrintStream {

private JTextArea consoleTextArea;

TextAreaPrintStream(JTextArea consoleTextArea) {

this.consoleTextArea = consoleTextArea;

}

@Override

public void println(String s) {

consoleTextArea.append(s + '/n');

}

//override other PrintStream methods you want to support in the same fashion

}

}

DaanSa at 2007-7-13 9:25:25 > top of Java-index,Security,Event Handling...
# 4

> >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

>

> Yes :)

>

> [code]public class MyFrameWithConsole extends JFrame

> {

>

>public MyFrameWithConsole() {

>JTextArea consoleTextArea = new JTextArea();

>add(consoleTextArea);

> System.setOut(new

> TextAreaPrintStream(consoleTextArea));

>}

> private static class TextAreaPrintStream extends

> PrintStream {

>private JTextArea consoleTextArea;

>TextAreaPrintStream(JTextArea consoleTextArea) {

> this.consoleTextArea = consoleTextArea;

>

>

> @Override

> public void println(String s) {

> consoleTextArea.append(s + '/n');

>

> //override other PrintStream methods you want

> to support in the same fashion

>}

> code]

The code has an error. This is because the superclass PrintStream doen't have a constructor that take no arguments.

Message was edited by:

marb19

marb19a at 2007-7-13 9:25:25 > top of Java-index,Security,Event Handling...