Подтвердить что ты не робот

Перенаправление System.out в JTextPane

У меня есть класс (показан ниже), который расширяет JPanel и содержит JTextPane. Я хочу перенаправить System.out и System.err на мой JTextPane. Мой класс, похоже, не работает. Когда я его запускаю, он перенаправляет системные отпечатки, но они не печатаются на моем JTextPane. Пожалуйста, помогите!

Примечание: Вызовы перенаправляются только при запуске приложения. Но в любое время после запуска вызовы System.out не перенаправляются на JTextPane. (т.е. если я помещаю в класс System.out.prinln();, он будет вызываться, но если он помещается в actionListener для последующего использования, он не перенаправляется).

public class OSXConsole extends JPanel {
    public static final long serialVersionUID = 21362469L;

    private JTextPane textPane;
    private PipedOutputStream pipeOut;
    private PipedInputStream pipeIn;


    public OSXConsole() {
        super(new BorderLayout());
        textPane = new JTextPane();
        this.add(textPane, BorderLayout.CENTER);

        redirectSystemStreams();

        textPane.setBackground(Color.GRAY);
        textPane.setBorder(new EmptyBorder(5, 5, 5, 5));

    }


    private void updateTextPane(final String text) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Document doc = textPane.getDocument();
                try {
                    doc.insertString(doc.getLength(), text, null);
                } catch (BadLocationException e) {
                    throw new RuntimeException(e);
                }
                textPane.setCaretPosition(doc.getLength() - 1);
            }
        });
    }


    private void redirectSystemStreams() {
      OutputStream out = new OutputStream() {
        @Override
        public void write(final int b) throws IOException {
          updateTextPane(String.valueOf((char) b));
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
          updateTextPane(new String(b, off, len));
        }

        @Override
        public void write(byte[] b) throws IOException {
          write(b, 0, b.length);
        }
      };

      System.setOut(new PrintStream(out, true));
      System.setErr(new PrintStream(out, true));
    }


}
4b9b3361

Ответ 1

Трубопроводные потоки всегда меня путают, поэтому мое решение Message Console не использует их. В любом случае, это моя попытка консоли, использующая потоки с потоками. Несколько отличий:

a) он использует JTextArea, потому что JTextArea более эффективен, чем JTextPane для простого отображения текста. Конечно, если вы намерены добавлять атрибуты в текст, вам нужна текстовая панель.

b) это решение использует Threads. Я уверен, что я где-то читал, что это необходимо для предотвращения блокировки вывода. В любом случае это работает в моем простом случае.

import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class Console implements Runnable
{
    JTextArea displayPane;
    BufferedReader reader;

    private Console(JTextArea displayPane, PipedOutputStream pos)
    {
        this.displayPane = displayPane;

        try
        {
            PipedInputStream pis = new PipedInputStream( pos );
            reader = new BufferedReader( new InputStreamReader(pis) );
        }
        catch(IOException e) {}
    }

    public void run()
    {
        String line = null;

        try
        {
            while ((line = reader.readLine()) != null)
            {
//              displayPane.replaceSelection( line + "\n" );
                displayPane.append( line + "\n" );
                displayPane.setCaretPosition( displayPane.getDocument().getLength() );
            }

            System.err.println("im here");
        }
        catch (IOException ioe)
        {
            JOptionPane.showMessageDialog(null,
                "Error redirecting output : "+ioe.getMessage());
        }
    }

    public static void redirectOutput(JTextArea displayPane)
    {
        Console.redirectOut(displayPane);
        Console.redirectErr(displayPane);
    }

    public static void redirectOut(JTextArea displayPane)
    {
        PipedOutputStream pos = new PipedOutputStream();
        System.setOut( new PrintStream(pos, true) );

        Console console = new Console(displayPane, pos);
        new Thread(console).start();
    }

    public static void redirectErr(JTextArea displayPane)
    {
        PipedOutputStream pos = new PipedOutputStream();
        System.setErr( new PrintStream(pos, true) );

        Console console = new Console(displayPane, pos);
        new Thread(console).start();
    }

    public static void main(String[] args)
    {
        JTextArea textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane( textArea );

        JFrame frame = new JFrame("Redirect Output");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( scrollPane );
        frame.setSize(200, 100);
        frame.setVisible(true);

        Console.redirectOutput( textArea );
        final int i = 0;

        Timer timer = new Timer(1000, new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                System.out.println( new java.util.Date().toString() );
                System.err.println( System.currentTimeMillis() );
            }
        });
        timer.start();
    }
}

Ответ 2

Message Console класс делает это для вас.

Edit:

Вот простой тестовый класс:

import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

public class MessageConsoleTest
{
    public static int counter;

    public static void main(String[] args)
        throws Exception
    {
        JTextComponent textComponent = new JTextPane();
        JScrollPane scrollPane = new JScrollPane( textComponent );

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Message Console");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.getContentPane().add( scrollPane );
        frame.setSize(400, 120);
        frame.setVisible(true);

        MessageConsole console = new MessageConsole(textComponent);
        console.redirectOut();
        console.redirectErr(Color.RED, null);

        Timer timer = new Timer(1000, new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent e)
            {
                System.out.println( new java.util.Date().toString() );
            }
        });
        timer.start();

        Thread.sleep(750);

        Timer timer2 = new Timer(1000, new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent e)
            {
                System.err.println( "Error Message: " + ++counter);
            }
        });
        timer2.start();
    }
}

Ответ 3

В следующей ссылке вы можете найти класс MessageConsole, о котором кто-то упоминал. Я внедрил программное обеспечение и использовал это решение, и оно прекрасно работает для меня. Я использовал инструмент проектирования Netbeans, поэтому код, касающийся внешнего вида JTextPane, немного громоздок, поэтому я не буду его здесь размещать.


JTextPane jTextPane = new JTextPane();

MessageConsole console = new MessageConsole(jTextPane);
/*
This parameters are optional, but if you are looking for a solution with JTextPane it is because you need them, at least color.
*/
console.redirectErr(Color.RED, null);
console.redirectOut();

//some event
private void jButton1ActionPerformed(ActionEvent evt) {
    /*
    In this event I execute a function of my business.
    I put it in a thread so that it does not block the graphical interface.
    There are many calls to System.out.println() and System.err.println()
    */
    BusinessClass bc = new BusinessClass();
    Runnable runnable = () -> {
        bc.someBusinessFn();
    };
    thread = new Thread(runnable);
    thread.start();
}

//My main method
public static void main(String args[]) {
        /* Create and display the GUI */
        EventQueue.invokeLater(() -> {
            new MyJFrame().setVisible(true);
        });
}

Edit

Извините, я не понял, что в ответе, подобном этому, они поместили ссылку на класс MessageConsole. Я этого не видел и тоже хотел показать свое решение.