Console em JTextArea System.setOut;

6 respostas
N

Boa tarde galera, estou desenvolvendo uma aplicação onde ela pode ser rodada em modo console e modo gráfico, e para o modo gráfico estou fazendo um console com o JTextArea. O modo que eu fiz printar o console é bastante simples, mudei o inputstream do programa para o JTextArea, mas por algum motivo caracteres unicode não são reconhecidos, aparecendo só um monte de letras sem sentido.
Segue meu código

OutputStream o = new OutputStream() { @Override public void write(int b) throws IOException { StringBuffer buf = new StringBuffer(buffer); if(b != '\n') { buf.append((char) b); buffer = buf.toString(); } else { insereLinha(buf.toString()); buffer = ""; } } }; System.setOut(new PrintStream(o)); System.setErr(new PrintStream(o));
Gostaria de saber como faria para que o System.out.println não fizesse aparecer essas letras sem sentido.

6 Respostas

Ruttmann

Dê uma olhada nessa thread do Stack Overflow: http://stackoverflow.com/questions/4747358/java-unicode-confusion

Vou fazer um resuminho, caso você não entenda inglês.

Basicamente o colega que respondeu lá explica que a JVM espera que o encoding de caracteres usado pelo console do sistema, seja o mesmo que o default do sistema. Mas o Windows usa 2 encodings diferentes (ANSI como default, e OEM para o console).

Então quando você tenta escrever caracteres Unicode, a JVM interpreta isso como sendo ANSI, e por isso aparecem os caracteres doidos.

Por fim ele recomenda não usar System.out.println, mas sim o código abaixo:

System.console().writer().println()

Não cheguei a testar essa solução, mas me parece bem sensata.

Teste aí e avise se funciona! :wink:

M

Da uma olhada aqui: http://javafree.uol.com.br/topic-874692-como-simular-um-console-com-jTextArea.html

N

Ruttmann:
Dê uma olhada nessa thread do Stack Overflow: http://stackoverflow.com/questions/4747358/java-unicode-confusion

Vou fazer um resuminho, caso você não entenda inglês.

Basicamente o colega que respondeu lá explica que a JVM espera que o encoding de caracteres usado pelo console do sistema, seja o mesmo que o default do sistema. Mas o Windows usa 2 encodings diferentes (ANSI como default, e OEM para o console).

Então quando você tenta escrever caracteres Unicode, a JVM interpreta isso como sendo ANSI, e por isso aparecem os caracteres doidos.

Por fim ele recomenda não usar System.out.println, mas sim o código abaixo:

System.console().writer().println()

Não cheguei a testar essa solução, mas me parece bem sensata.

Teste aí e avise se funciona! ;)

Tentei e infelizmente não funcionou, fica dando NullPointerException. =/

Ruttmann

Nicksf13:
Ruttmann:
Dê uma olhada nessa thread do Stack Overflow: http://stackoverflow.com/questions/4747358/java-unicode-confusion

Vou fazer um resuminho, caso você não entenda inglês.

Basicamente o colega que respondeu lá explica que a JVM espera que o encoding de caracteres usado pelo console do sistema, seja o mesmo que o default do sistema. Mas o Windows usa 2 encodings diferentes (ANSI como default, e OEM para o console).

Então quando você tenta escrever caracteres Unicode, a JVM interpreta isso como sendo ANSI, e por isso aparecem os caracteres doidos.

Por fim ele recomenda não usar System.out.println, mas sim o código abaixo:

System.console().writer().println()

Não cheguei a testar essa solução, mas me parece bem sensata.

Teste aí e avise se funciona! ;)

Tentei e infelizmente não funcionou, fica dando NullPointerException. =/

NullPointer onde?

Posta aqui a stack da exceção por favor.

N
Exception in thread AWT-EventQueue-0 java.lang.NullPointerException

at telas.TelaPrincipal.validaComando(TelaPrincipal.java:129)

at telas.TelaPrincipal$2.actionPerformed(TelaPrincipal.java:78)

at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)

at javax.swing.DefaultButtonModel.setPressed(Unknown Source)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)

at java.awt.Component.processMouseEvent(Unknown Source)

at javax.swing.JComponent.processMouseEvent(Unknown Source)

at java.awt.Component.processEvent(Unknown Source)

at java.awt.Container.processEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)

at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Window.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEventImpl(Unknown Source)

at java.awt.EventQueue.access$300(Unknown Source)

at java.awt.EventQueue$3.run(Unknown Source)

at java.awt.EventQueue$3.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)

at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)

at java.awt.EventQueue$4.run(Unknown Source)

at java.awt.EventQueue$4.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)
ViniGodoy

Eu fiz um componente para isso:

Se usar um JTextPane, o System.err sai em vermelho.

Já tem tratamento também para caso o stream esteja numa thread secundária.

Criado 15 de julho de 2015
Ultima resposta 25 de jul. de 2015
Respostas 6
Participantes 4