Console to GUI

?

-

I have a class A that has a method that does something and then prints
to a console:

public class A {

public A() {
...
}

public void handleMessage(Message message) {
String command = message.getCommand();

if (command.equals("A")) {
handleA(message);
} else if (command.equals("B")) {
handleB(message);
...
...
...
} else if (command.equals("Z")) {
handleZ(message);
}
}

protected void handleA(Message message) {
... do stuff
System.out.println(text);
}

protected void handleB(Message message) {
... do other stuff
System.out.println(text1);
}

...
...

protected void handleZ(Message message) {
... do some other stuff
System.out.println(text);
}
}

'text', 'text1', ... 'text2' are varying text based on
message.getParameters().


now i would like to print the 'text', 'text1', ... 'text2' inside a
JTextPane and use different colors for the text.

One way I can think of is to subclass A:

public class GUIA extends A {

public GUIA() {
...
}

public void handleMessage(Message message) {
String command = message.getCommand();

if (command.equals("A")) {
handleA(message);
} else if (command.equals("B")) {
handleB(message);
...
...
...
} else if (command.equals("Z")) {
handleZ(message);
}
}

protected void handleA(Message message) {
super.handleA(message);
jTextPane.insertString(...);
}

protected void handleB(Message message) {
super.handleB(message);
jTextPane.insertString(...);
}

...
...

protected void handleZ(Message message) {
super.handleZ(message);
jTextPane.insertString(...);
}
}

Three questions:

1) Is it supposed to be subclassed?
2) If no, what is the correct way?
3) If yes, how do I 'silenced' the System.out.println() or redirect it etc.

Thank you in advance.
 
?

-

jTextPane.insertString(...);

should be jTextPane.getDocument().insertString(...); but that's not
important.
 
A

Arnaud Berger

Hi,

This looks alright.

However, why would you call super.handleXXX(message) if you don't want the
superclass to do its
processing (i.e println) ?

When overriding a method, you are not forced at all to call the one of the
superclass.

One last note : standard and error outputstream may be redirected with
System.setErr(PrintStream err) and
System.setOut(PrintStream out), but the effect is VM-wide.

Regards,

Arnaud
 
?

-

Arnaud said:
However, why would you call super.handleXXX(message) if you don't want the
superclass to do its
processing (i.e println) ?

I'm calling super.handleXXX so that the logic in the superclass, except
for the System.out.println, is executed. Like for instance, handleA()
will create an Apple class, handleB() will create a Ball class so on and
so forth. Hence, I need to 'silenced' the println.
 
?

-

- said:
I'm calling super.handleXXX so that the logic in the superclass, except
for the System.out.println, is executed. Like for instance, handleA()
will create an Apple class, handleB() will create a Ball class so on and
so forth. Hence, I need to 'silenced' the println.

btw, is subclassing it really the way to go?
 
?

-

- said:
btw, is subclassing it really the way to go?

so if a Main class is the one that prints to System.out, what I should
do is to subclass the model to create the view?

public class App extends Main {
....
}
 
A

Arnaud Berger

Your class A doesn't look like a "model" though.

The real model seems to be the messages themselves.

Class A is acting depending on , say, the "model changes" and is for me
nearer to
a controller than a model (and the controller knows sees both the model and
the view).

The view would be the final JTextPane.

Anyone correct me if something sounds wrong.

Regards,

Arnaud
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top