Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Question/Problem about swing jTextArea to be used as console I/O

$
0
0
Greetings..
I'm try to run a java class intended to be executed in the console in a swing.
I managed to come up with the outputs though, using PipedInputStream with System.setOut() and System.err()and posting them in the jTextArea using it's .setText() method.
The problem is I can't find, or I just don't know what I'm looking for, a way to input through the jTextArea.
I know for one that you need to use a key listener for this to work.
I'm guessing something like, use some sort of a container, input from jTextArea and then relay it to the input stream or something.

Here are the snippets.

Swing app

        
        String[] arg = new String[] {""};
        public Thread reader;
	public Thread reader2;
	public boolean quit;
        public final PipedInputStream pin=new PipedInputStream();
	public final PipedInputStream pin2=new PipedInputStream();

    public DesktopApplication1View(SingleFrameApplication app){

//<editor-fold desc="DO NOT DELETE OR MODIFY">
        super(app);

        initComponents();
        
        jTextArea2.setLineWrap(true);

        //</editor-fold>

        //<editor-fold desc="console outputs (try..catch)">
                try
		{
			PipedOutputStream pout=new PipedOutputStream(this.pin);
			System.setOut(new PrintStream(pout,true));
		}
		catch (java.io.IOException io)
		{
			jTextArea2.setText("Couldn't redirect STDOUT to this console\n"+io.getMessage());
		}
		catch (SecurityException se)
		{
			jTextArea2.setText("Couldn't redirect STDOUT to this console\n"+se.getMessage());
                }

		try
		{
			PipedOutputStream pout2=new PipedOutputStream(this.pin2);
			System.setErr(new PrintStream(pout2,true));
		}
		catch (java.io.IOException io)
		{
			jTextArea2.setText("Couldn't redirect STDERR to this console\n"+io.getMessage());
		}
		catch (SecurityException se)
		{
			jTextArea2.setText("Couldn't redirect STDERR to this console\n"+se.getMessage());
                }

		quit=false; // signals the Threads that they should exit

		// Starting two seperate threads to read from the PipedInputStreams
		//output
		reader=new Thread(this);
		reader.setDaemon(true);
		reader.start();
		//error
		reader2=new Thread(this);
		reader2.setDaemon(true);
		reader2.start();
        //</editor-fold>


    System.out.print(System.getProperty("os.name"));
        
        jButton1.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){

                jTextArea2.setText("");

                   oddeven.main(arg);
            }
        });

        
    }

    public synchronized void run()
	{
		try
		{
			while (Thread.currentThread()==reader)
			{
				try { this.wait(100);}catch(InterruptedException ie) {}
				if (pin.available()!=0)
				{
					String input=this.readLine(pin);
					jTextArea2.setText(input);
				}
				if (quit) return;
			}

			while (Thread.currentThread()==reader2)
			{
				try { this.wait(100);}catch(InterruptedException ie) {}
				if (pin2.available()!=0)
				{
					String input=this.readLine(pin2);
					jTextArea2.setText(input);
				}
				if (quit) return;
			}
		} catch (Exception e)
		{
			jTextArea2.setText("\nConsole reports an Internal error.");
			jTextArea2.setText("The error is: "+e);
		}

	}

    public synchronized String readLine(PipedInputStream in) throws IOException
	{
		String input="";
		do
		{
			int available=in.available();
			if (available==0) break;
			byte b[]=new byte[available];
			in.read(B)/>;
			input=input+new String(b,0,b.length);
		}while( !input.endsWith("\n") &&  !input.endsWith("\r\n") && !quit);
		return input;
	}



oddeven.java(java class intended for console)


import java.io.*;

public class oddeven{
	public static void main(String[]args) throws Exception{
		odd();
	}
	public static void odd() throws Exception{
            double num = 0;
            int even = 0, odd = 0, zero = 0;
		try{
			BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
			System.out.print("Enter numbers\n(Input negative value to end)\n");
			num = Double.parseDouble(dataIn.readLine());
			while(num>=0){
				if(num == 0)
					zero++;
				else if(num%2==0)
					even++;
				else
					odd++;
				num = Double.parseDouble(dataIn.readLine());
			}
			System.out.print("There are\n"+even+" even numbers\n"+odd+" odd numbers\n"+zero+" zero value");

		} catch(NumberFormatException e){
			System.out.print("Wrong input, enter again\n");
			odd();
		}
	}
}



It works though, by the use of the IDE's console. I mean, if I run the swing app and launch/call the class, the jTextArea, the whole swing app freezes, unless I input on the IDE's console.
I'm using NetBeans 6.9.1

Thank you.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>