REDIRECTION DES FLUX SYSTEM.OUT ET SYSTEM.ERR DANS UNE JTEXTAREA

uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 - 24 oct. 2007 à 19:17
roidec Messages postés 7 Date d'inscription jeudi 14 février 2008 Statut Membre Dernière intervention 28 avril 2008 - 31 août 2010 à 15:10
Cette discussion concerne un article du site. Pour la consulter dans son contexte d'origine, cliquez sur le lien ci-dessous.

https://codes-sources.commentcamarche.net/source/44400-redirection-des-flux-system-out-et-system-err-dans-une-jtextarea

roidec Messages postés 7 Date d'inscription jeudi 14 février 2008 Statut Membre Dernière intervention 28 avril 2008
31 août 2010 à 15:10
Merci c'est très intéressant
herve91fr Messages postés 5 Date d'inscription vendredi 16 juillet 2004 Statut Membre Dernière intervention 19 mai 2008
13 mai 2008 à 22:13
Pourquoi faire si compliqué ?

JTextArea ta = new JTextArea(16, 80);
PrintStream ps = new PrintStream(new TextAreaOutputStream(ta));
System.setOut(ps);
System.setErr(ps);

class TextAreaOutputStream extends OutputStream {

private JTextArea ta;

public TextAreaOutputStream(JTextArea ta) {
this.ta = ta;
}

public synchronized void write(int b) throws IOException {
ta.append(String.valueOf((char) b));
}
}
komes28 Messages postés 2 Date d'inscription lundi 26 novembre 2007 Statut Membre Dernière intervention 4 décembre 2007
4 déc. 2007 à 07:09
merci beaucoup pour votre prompt reponse...
twinser Messages postés 32 Date d'inscription samedi 13 octobre 2007 Statut Membre Dernière intervention 26 janvier 2009 1
3 déc. 2007 à 19:30
Bien sur, c'est meme le but de cette console, voici un exemple pour intégrer la console :
/*
* Test.java
* Ce fichier permet de tester Console.java qui est une JInternalFrame
* Il est possible d'utiliser Console.java en tant que Frame ou JDialog, il suffit
* de modifier l'héritage.
* Remarque : les flux ne sont pas réinitialisés à la fermeture de la JInternalFrame.
* Attention au package, les deux classes sont mises dans le package par défaut.
*/

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
* @author ronan
*/
public class Test {
public Test() {
// Préparation d'une JFrame'
JFrame frame = new JFrame("Test");
frame.setSize(600,600);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(SwingUtilities.getRoot(frame));
JDesktopPane desktop = new JDesktopPane();
frame.add(desktop);

Console console = new Console();
desktop.add(console);

frame.setVisible(true);

System.out.println("un petit test");
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Test();
}
}
komes28 Messages postés 2 Date d'inscription lundi 26 novembre 2007 Statut Membre Dernière intervention 4 décembre 2007
3 déc. 2007 à 16:17
Je ne vois pas comment le code fonctionne. Je vais vous expliquer ce que je veux faire et vous me diriez si c est possible avec ce code. Je veux créer une console de débogage pour un logiciel. c a dire que tout les strings générer pas mon code sont system.out (afficher) en console. J aimerais savoir si je peux integrer cette console a mon code de facon a les voir directement. Un exemple serait bien apprécier. Je vous remercie d'avance
uhrand Messages postés 491 Date d'inscription samedi 20 mai 2006 Statut Membre Dernière intervention 15 juillet 2012 9
24 oct. 2007 à 19:17
J'ai beaucoup apprécié cette source.
Pour afficher les erreurs en rouge, il faudra prendre une JTextPane. Exemple:
import javax.swing.text.*;
...
    private JTextPane textPane;
    private StyledDocument textArea;
    private SimpleAttributeSet red;
...
    public Console() {
...
        textPane = new JTextPane();
        textArea = textPane.getStyledDocument();
        red = new SimpleAttributeSet();
        StyleConstants.setForeground(red, Color.red);
...
    }
...
            try {
                textArea.insertString(textArea.getLength(), "Couldn't redirect STDOUT to this console\n" + io.getMessage(), null);
            } catch (BadLocationException exception) {            }
...
            try {
                textArea.insertString(textArea.getLength(), "Couldn't redirect STDOUT to this console\n" + se.getMessage(), null);
            } catch (BadLocationException exception) {            }
...
            try {
                textArea.insertString(textArea.getLength(), "Couldn't redirect STDERR to this console\n" + io.getMessage(), null);
            } catch (BadLocationException exception) {            }
...
            try {
                textArea.insertString(textArea.getLength(), "Couldn't redirect STDERR to this console\n" + se.getMessage(), null);
            } catch (BadLocationException exception) {            }
...
    public synchronized void actionPerformed(ActionEvent evt) {
        try {
            textArea.remove(0, textArea.getLength());
        } catch (BadLocationException exception) {        }
    }
...
    public synchronized void run() {
        try {
            while (Thread.currentThread() == reader) {
...
                    textArea.insertString(textArea.getLength(), input, null);
...
            }
            while (Thread.currentThread() == reader2) {
...
                    textArea.insertString(textArea.getLength(), input, red);
...
            }
        } catch (Exception e) {
            try {
                textArea.insertString(textArea.getLength(), "\nConsole reports an Internal error.", null);
                textArea.insertString(textArea.getLength(), "The error is: " + e, null);
            } catch (BadLocationException exception) {            }
        }
...
Rejoignez-nous