पर सिस्टम को रीडायरेक्ट करना। मेरे पास एक कक्षा है (नीचे दिखाया गया है) जो JPanel
तक फैला है और इसमें JTextPane
है। मैं System.out
और System.err
को अपने JTextPane
पर रीडायरेक्ट करना चाहता हूं। मेरी कक्षा काम नहीं कर रही है। जब मैं इसे चलाता हूं, यह सिस्टम प्रिंट को रीडायरेक्ट करता है, लेकिन वे मेरे 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));
}
}
मैंने अपना जवाब हटा दिया क्योंकि यह गलत था। – jjnguy
क्या आप कुछ कॉल को रीडायरेक्ट कर रहे हैं कुछ जवाब नहीं दे रहे हैं? – jjnguy
केवल ओएसएक्सकंसोल कक्षा के अंदर से कॉल को JTextPane पर मुद्रित किया जाता है। – Jakir00