Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.java.gui > #5514

how to direct console output to a textarea

Newsgroups comp.lang.java.gui
Date 2017-01-04 23:56 -0800
Message-ID <163b0e03-2224-427a-910a-e9ef41df36cf@googlegroups.com> (permalink)
Subject how to direct console output to a textarea
From abhprk3926@gmail.com

Show all headers | View raw


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<Void, String>() {
			protected Void doInBackground() throws Exception {
				Scanner s = new Scanner(out);
				while (s.hasNextLine()) publish(s.nextLine() + "\n");
				return null;
			}
			protected void process(ArrayList<String> 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;
	}
}

Back to comp.lang.java.gui | Previous | NextNext in thread | Find similar


Thread

how to direct console output to a textarea abhprk3926@gmail.com - 2017-01-04 23:56 -0800
  Re: how to direct console output to a textarea Nigel Wade <nmw@local.domain> - 2017-01-05 10:35 +0000
    Re: how to direct console output to a textarea abhprk3926@gmail.com - 2017-01-05 03:04 -0800
      Re: how to direct console output to a textarea Nigel Wade <nmw@local.domain> - 2017-01-09 12:11 +0000
      Re: how to direct console output to a textarea kanisfatemashanta28@gmail.com - 2018-12-21 00:35 -0800
  Re: how to direct console output to a textarea Abhay Prakash <abhprk3926@gmail.com> - 2017-01-09 07:17 -0800
    Re: how to direct console output to a textarea kanisfatemashanta28@gmail.com - 2018-12-18 11:30 -0800

csiph-web