X-Received: by 10.13.250.129 with SMTP id k123mr23356680ywf.39.1483602974349; Wed, 04 Jan 2017 23:56:14 -0800 (PST) X-Received: by 10.157.14.183 with SMTP id 52mr519672otj.20.1483602974304; Wed, 04 Jan 2017 23:56:14 -0800 (PST) Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder1.iad1.usenetexpress.com!216.166.98.84.MISMATCH!border1.nntp.dca1.giganews.com!nntp.giganews.com!c47no174778qtc.1!news-out.google.com!g131ni7314itg.0!nntp.google.com!b123no451102itb.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.gui Date: Wed, 4 Jan 2017 23:56:14 -0800 (PST) Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=47.31.26.200; posting-account=2uSbDQoAAACyUbc19SW_EKUfonn1OvrU NNTP-Posting-Host: 47.31.26.200 User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <163b0e03-2224-427a-910a-e9ef41df36cf@googlegroups.com> Subject: how to direct console output to a textarea From: abhprk3926@gmail.com Injection-Date: Thu, 05 Jan 2017 07:56:14 +0000 Content-Type: text/plain; charset=UTF-8 Lines: 125 Xref: csiph.com comp.lang.java.gui:5514 i cant seem to figure a way to display console output to a textarea. here is what i have done till now. any help would be appreciated. import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; class test extends JFrame { JTextArea content; JSplitPane pane; JMenuBar jmb = new JMenuBar(); JMenu menu = new JMenu("Options"); JMenuItem item = new JMenuItem("Compile") , item1 = new JMenuItem("Run") , item2 = new JMenuItem("Save"); test() { try { PipedInputStream in = new PipedInputStream(); PipedInputStream out = new PipedInputStream(); System.setIn(in); System.setOut(new PrintStream(new PipedOutputStream(out) , true)); PrintWriter writer = new PrintWriter(new PipedOutputStream(in),true); setTitle("Testing Window"); setSize(700,700); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); content = new JTextArea(); pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,new JScrollPane(content),new JScrollPane(compiler(out,writer))); pane.setResizeWeight(0.8); add(pane); menu.add(item); menu.add(item1); menu.add(item2); jmb.add(menu); setJMenuBar(jmb); }catch(Exception e){} ActionListener listener = (ActionEvent ae) -> { try(FileWriter file = new FileWriter("hello.java"); BufferedWriter bw = new BufferedWriter(file)) { Scanner sc = new Scanner(content.getText()); while( sc.hasNext()) { bw.write(sc.nextLine()); bw.newLine(); } }catch(Exception e){e.printStackTrace();} }; item2.addActionListener(listener); ActionListener listener1 = (ActionEvent ae)->{ try { Process p = Runtime.getRuntime().exec("javac hello.java"); Scanner sc = new Scanner(p.getInputStream()); p.waitFor(); }catch(Exception e){e.printStackTrace();} }; item.addActionListener(listener1); ActionListener listener2 = (ActionEvent ae)->{ try { Process p = Runtime.getRuntime().exec("java hello"); p.waitFor(); }catch(Exception e){e.printStackTrace();} }; item1.addActionListener(listener2); setVisible(true); } public static void main(String args[]) { SwingUtilities.invokeLater( ()->{new test();} ); } public static JTextArea compiler(final InputStream out, final PrintWriter in) { final JTextArea area = new JTextArea(); // handle "System.out" new SwingWorker() { protected Void doInBackground() throws Exception { Scanner s = new Scanner(out); while (s.hasNextLine()) publish(s.nextLine() + "\n"); return null; } protected void process(ArrayList chunks) { for (String line : chunks) area.append(line); } }.execute(); // handle "System.in" area.addKeyListener(new KeyAdapter() { private StringBuffer line = new StringBuffer(); @Override public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if (c == KeyEvent.VK_ENTER) { in.println(line); line.setLength(0); } else if (c == KeyEvent.VK_BACK_SPACE) { line.setLength(line.length() - 1); } else if (!Character.isISOControl(c)) { line.append(e.getKeyChar()); } } }); return area; } }