2010-12-14 15 views
9

पर सिस्टम को रीडायरेक्ट करना। मेरे पास एक कक्षा है (नीचे दिखाया गया है) जो 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)); 
    } 


} 
+0

मैंने अपना जवाब हटा दिया क्योंकि यह गलत था। – jjnguy

+0

क्या आप कुछ कॉल को रीडायरेक्ट कर रहे हैं कुछ जवाब नहीं दे रहे हैं? – jjnguy

+0

केवल ओएसएक्सकंसोल कक्षा के अंदर से कॉल को JTextPane पर मुद्रित किया जाता है। – Jakir00

उत्तर

7

पाइप्ड हमेशा मुझे भ्रमित स्ट्रीम, जिसके कारण मेरी संदेश कंसोल समाधान उन्हें का उपयोग नहीं करता:

यहाँ एक साधारण परीक्षण वर्ग है। वैसे भी यहां पाइप स्ट्रीम का उपयोग कर कंसोल पर मेरा प्रयास है। कुछ अंतर:

ए) यह एक JTextArea का उपयोग करता है क्योंकि एक JTextArea केवल टेक्स्ट प्रदर्शित करने के लिए JTextPane से अधिक कुशल है। बेशक यदि आप पाठ में विशेषताओं को जोड़ना चाहते हैं तो आपको एक टेक्स्ट फलक की आवश्यकता है।

बी) यह समाधान थ्रेड का उपयोग करता है। मुझे यकीन है कि मैंने कहीं पढ़ा है कि आउटपुट को अवरुद्ध करने के लिए यह आवश्यक था। वैसे भी यह मेरे सरल परीक्षण मामले में काम करता है।

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(); 
    } 
} 
7

Message Console कक्षा आपके लिए यह करती है।

संपादित करें:

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(); 
    } 
} 
+1

मैंने कोशिश की, लेकिन ऐसा लगता है कि यह बिल्कुल काम नहीं करता है। – Jakir00

+0

@ जैकोब, यदि आपके पास एक साधारण एसएससीसीई (http://sscce.org) है जो दर्शाता है कि यह काम नहीं कर रहा है तो आप मुझे देखने के लिए कुछ कोड भेजने के लिए वेबसाइट के "हमसे संपर्क करें" पृष्ठ का उपयोग कर सकते हैं। – camickr

+0

यह काम करता है अगर आप इसे सही इस्तेमाल करते हैं। हालांकि, लाइव आउटपुट प्राप्त करने के लिए आपको स्विंगवर्कर पर किसी भी लंबे समय से चलने वाले कार्यों को रखने की आवश्यकता होगी। – CaffeineToCode

संबंधित मुद्दे