Results 1 to 7 of 7
  1. #1
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default No output displaying

    ok so i have a program that takes 3 threads and syncs them(got this working) then the output needs to be displayed in a JTextArea. I have everything set up and working except the output doesnt display in the JTextArea.

    Java Code:
    import java.util.concurrent.*;
    import java.util.concurrent.locks.*;
    import java.awt.*;
    import javax.swing.*;
    import java.lang.*;
    import java.io.*;
    import java.util.*;
    import java.awt.event.*;
    import java.awt.Graphics;
    import java.util.Scanner;
    import javax.swing.JTextArea.*;
    
    public class TaskThreadDemo extends JFrame{
      TaskThreadDemo(){
    	  outputPanel p1 = new outputPanel();
      	  setLayout(new FlowLayout());
    
      	  add(p1);
      }
    
      public static void main(String[] args) {
    
    
        // Create tasks
        Runnable printA = new PrintChar('a', 100);
        Runnable printB = new PrintChar('b', 100);
        Runnable print100 = new PrintNum(100);
    
        // Create threads
        Thread thread1 = new Thread(printA);
        Thread thread2 = new Thread(printB);
        Thread thread3 = new Thread(print100);
    
        // Start threads
        thread1.start();
        thread2.start();
        thread3.start();
    
    
        TaskThreadDemo frame = new TaskThreadDemo();
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("lab9");
        frame.setVisible(true);
        frame.setSize(500,500);
    
      }
    
    
    }
    class Output{
    		char input1;
    		int input2;
    		String out = " ";
    
    		Output(){
    		}
    
    
    		public Output(char text, int n)
    		{
    			this.input1 = text;
    			this.input2 = n;
    		}
    
    		public void setOutput(char text, int n){
    			String internal = " ";
    
    
    			if (text != '\\')
    			{
    				internal = Character.toString(text);
    				out +=internal;
    				internal = " ";
    			}
    			if (n != 0)
    			{
    				internal = Integer.toString(n);
    				out += internal;
    				internal = " ";
    			}
    			this.out = out;
    		}
    		public String getOutput(){
    			return out;
    		}
    }
    
    class outputPanel extends JPanel{
    	Output outtext = new Output();
    
    	String text = outtext.getOutput();
    
    	outputPanel()
    	{
    		JTextArea jtaOutput = new JTextArea(text,10,20);
    		jtaOutput.setLineWrap(true);
    		jtaOutput.setWrapStyleWord(true);
    		jtaOutput.setEditable(false);
    
    		JScrollPane scrollPane = new JScrollPane(jtaOutput);
    		setLayout(new FlowLayout());
    		add(scrollPane);
    	}
    }
    // The task for printing a specified character in specified times
    class PrintChar implements Runnable
    {
    
      	Output output = new Output();
      	private char charToPrint; // The character to print
      	private int times; // The times to repeat
      	private static Lock lock = new ReentrantLock();
    
    
      	/** Construct a task with specified character and number of
      	 *  times to print the character
      	 */
      	public PrintChar(char c, int t)
      	{
      	  charToPrint = c;
      	  times = t;
      	}
    
      	/** Override the run() method to tell the system
      	 *  what the task to perform
      	 */
      	public void  run()
      	{
    		lock.lock();
      		try{
    				Thread.sleep(100);
    				for (int i = 0; i < times; i++)
    				{
          				output.setOutput(charToPrint, 0);
        			}
    			}
    		catch (InterruptedException ex){
    			ex.printStackTrace();
    		}
    
     		finally
     		{
    			lock.unlock();
    		}
    	}
    }
    
    // The task class for printing number from 1 to n for a given n
    class PrintNum implements Runnable
    {
    	Output output = new Output();
    	private int lastNum;
    	private static Lock lock = new ReentrantLock();
    
      	/** Construct a task for printing 1, 2, ... i */
      	public PrintNum(int n)
      	{
      	  lastNum = n;
      	}
    
      	/** Tell the thread how to run */
      	public void run()
      	{
      	  lock.lock();
      	  try{
    		  	for (int i = 1; i <= lastNum; i++)
      	  		{
          			output.setOutput('\\', i);
        		}
    		}
    	  finally{
    		  lock.unlock();
    	  }
    	}
    }

  2. #2
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    try to send the JTextArea's object into a Runnable class....

    inside the run() method, do the operation (output to textArea) by using the received object.
    freedom exists in the world of ideas

  3. #3
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default

    how do you mean sukatoa?

  4. #4
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    How? The code? ok.....

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    
    public class test extends JFrame{
    
    	private JTextArea t = new JTextArea();	
    
    	public static void main(String args[]){
    		new test().setVisible(true);
    	}
    
    	public test(){
    		this.setBounds(100,100,150,100);
    		this.add(new JScrollPane().add(t));
    		this.setDefaultCloseOperation(3);
    		new Thread(new Output(t,new String[]{"I","am","a","Java","Programmer"})).start();
    	}
    }
    
    class Output implements Runnable{
    
    	JTextArea refArea;
    	String[] text;
    
    	public Output(JTextArea temp, String[] texts){
    		refArea = temp;
    		text = texts;
    	}
    
    	@Override public void run(){
    		boolean b = false;
    		int len = 0;
    		while(!b){
    			try{
    				new java.awt.EventQueue().invokeLater(new Runnable(){
    					@Override public void run(){
    						refArea.setVisible(true);
    					}
    				});
    				Thread.sleep(1000);
    				refArea.append(text[len].concat(" "));
    				len++;
    
    				if(len == text.length)
    					b = true;
    			}catch(InterruptedException ie){}
    		}
    	}
    }
    ....

    Just an example....
    freedom exists in the world of ideas

  5. #5
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default

    I guess i havent been to clear, as i understand what your doing there it doesnt click with me for some reason.

    In my code the PrintChar, Printnum, and MainMethod cannot be changed at all. The rest if it is open to be changed.

  6. #6
    sukatoa's Avatar
    sukatoa is offline Senior Member
    Join Date
    Jan 2008
    Location
    Cebu City, Philippines
    Posts
    556
    Rep Power
    7

    Default

    Yah... my idea is conflict to your design....

    Have you check the output in console?
    freedom exists in the world of ideas

  7. #7
    Rgfirefly24 is offline Member
    Join Date
    Aug 2007
    Posts
    20
    Rep Power
    0

    Default

    well here is what i've done. i've redone the code mostly with the approval of my teacher and the help of someone and come up with this:

    Java Code:
    import java.util.concurrent.*;
    import java.util.concurrent.locks.*;
    import java.awt.*;
    import javax.swing.*;
    import java.lang.*;
    import java.io.*;
    import java.util.*;
    import java.awt.event.*;
    import java.awt.Graphics;
    import java.util.Scanner;
    import javax.swing.JTextArea.*;
    
    public class TaskThreadDemo extends JFrame{
      TaskThreadDemo(){
    	 	 super("Output Stream to Text component");
    	 	 JTextArea jtaOutput = new JTextArea();
    	 	 TextComponentOutputStream text = new TextComponentOutputStream(jtaOutput);
    
    	     super.setDefaultCloseOperation(EXIT_ON_CLOSE);
    	     JTextPane mypane = new JTextPane();
    	     PrintStream ps = new PrintStream(text);
    	     JScrollPane scroller = new JScrollPane(mypane);
    	     super.getContentPane().add(scroller);
    	     super.pack();
        	 super.setVisible(true);
    
        	 // Create tasks
        	Runnable printA = new PrintChar(ps, 'a', 100);
        	Runnable printB = new PrintChar(ps, 'b', 100);
        	Runnable print100 = new PrintNum(ps, 100);
    
        	// Create threads
        	Thread thread1 = new Thread(printA);
        	Thread thread2 = new Thread(printB);
        	Thread thread3 = new Thread(print100);
    
        	// Start threads
        	thread1.start();
        	thread2.start();
        	thread3.start();
      }
    
      public static void main(String[] args) {
    		try{
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }catch(Exception e){}
        new TaskThreadDemo();
    }
    
    
    }
    /*class Output{
    
    		String out1, out2;
    
    		Output(){
    		}
    		public void setcharOutput(char[] temp){
    
    		}
    		public String getcharOutput(){
    			//System.out.println(out);//used for checking output at this point
    			return out2;
    		}
    		public void setintOutput(String temp){
    				this.out1 = temp;
    
    			}
    
    
    		public String getintOutput(){
    			System.out.print(out1);
    			return out1;
    		}
    
    	}*/
    
    /*class outputPanel extends JPanel{
    
    
    	outputPanel()
    	{
    		JTextArea jtaOutput = new JTextArea(10,20);
    		jtaOutput.setLineWrap(true);
    		jtaOutput.setWrapStyleWord(true);
    		jtaOutput.setEditable(false);
    		JScrollPane scrollPane = new JScrollPane(jtaOutput);
    		setLayout(new FlowLayout());
    		add(scrollPane);
    		PrintStream ps = new PrintStream(new TextComponentOutputStream(jtaOutput));
    	}
    }*/
    // The task for printing a specified character in specified times
    class PrintChar implements Runnable
    {
    
      	private PrintStream ps;
    	private String name;
    	private char charToPrint; // The character to print
      	private int times; // The times to repeat
      	private static Lock lock = new ReentrantLock();
    
    
      	/** Construct a task with specified character and number of
      	 *  times to print the character
      	 */
      	public PrintChar(PrintStream ps, char c, int t)
      	{
      	  this.ps = ps;
      	  charToPrint = c;
      	  times = t;
      	}
    
      	/** Override the run() method to tell the system
      	 *  what the task to perform
      	 */
      	public void  run()
      	{
    		lock.lock();
      		try{
    				Thread.sleep(100);
    				for (int i = 0; i < times; i++)
    				{
          				this.ps.println(charToPrint);
          				//System.out.print(charToPrint);//used for checking output at this level
        			}
    			}
    		catch (InterruptedException ex){
    			ex.printStackTrace();
    		}
    
     		finally
     		{
    			lock.unlock();
    		}
    	}
    }
    
    // The task class for printing number from 1 to n for a given n
    class PrintNum implements Runnable
    {
    
    	private PrintStream ps;
    	private int lastNum;
    	private static Lock lock = new ReentrantLock();
    
      	/** Construct a task for printing 1, 2, ... i */
      	public PrintNum(PrintStream ps, int n)
      	{
      	  lastNum = n;
      	}
    
      	/** Tell the thread how to run */
      	public void run()
      	{
      	  String strtemp = " ";
      	  lock.lock();
      	  	  	for (int i = 1; i < lastNum; i++)
      	  		{
          			this.ps.println(" " + i);
          			//System.out.print(" " + i);//used for checking output at this point
        		}
    
    
    		  lock.unlock();
    
    	}
    }
    and
    Java Code:
    import java.io.*;
    import javax.swing.text.JTextComponent;
    
    public class TextComponentOutputStream extends OutputStream{
    
      private JTextComponent component;
    
      public TextComponentOutputStream(JTextComponent component){
        super();
        this.component = component;
      }
    
      public void write(int b)throws IOException{
        this.component.setText(this.component.getText()+(char)b);
      }
    
    
    
    }

Similar Threads

  1. problem displaying GUI from jbase
    By diveshsingh in forum AWT / Swing
    Replies: 0
    Last Post: 01-30-2008, 09:07 PM
  2. displaying string
    By jamborta in forum AWT / Swing
    Replies: 6
    Last Post: 01-23-2008, 08:15 PM
  3. Displaying characters in many ways.
    By TampaTechGuy in forum New To Java
    Replies: 7
    Last Post: 01-02-2008, 10:16 PM
  4. Displaying charts in a jsp
    By Priyanka in forum Java Servlet
    Replies: 1
    Last Post: 11-16-2007, 11:59 AM
  5. Images not displaying in JSP in IE7
    By chadscc in forum Advanced Java
    Replies: 0
    Last Post: 11-13-2007, 04:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •