Results 1 to 7 of 7
Thread: No output displaying
- 04-27-2008, 02:34 PM #1
Member
- Join Date
- Aug 2007
- Posts
- 20
- Rep Power
- 0
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(); } } }
- 04-27-2008, 05:29 PM #2
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
- 04-27-2008, 05:55 PM #3
Member
- Join Date
- Aug 2007
- Posts
- 20
- Rep Power
- 0
how do you mean sukatoa?
- 04-27-2008, 06:17 PM #4
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
- 04-27-2008, 07:12 PM #5
Member
- Join Date
- Aug 2007
- Posts
- 20
- Rep Power
- 0
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.
- 04-27-2008, 07:40 PM #6
Yah... my idea is conflict to your design....
Have you check the output in console?freedom exists in the world of ideas
- 04-27-2008, 08:37 PM #7
Member
- Join Date
- Aug 2007
- Posts
- 20
- Rep Power
- 0
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:
andJava 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(); } }
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
-
problem displaying GUI from jbase
By diveshsingh in forum AWT / SwingReplies: 0Last Post: 01-30-2008, 08:07 PM -
displaying string
By jamborta in forum AWT / SwingReplies: 6Last Post: 01-23-2008, 07:15 PM -
Displaying characters in many ways.
By TampaTechGuy in forum New To JavaReplies: 7Last Post: 01-02-2008, 09:16 PM -
Displaying charts in a jsp
By Priyanka in forum Java ServletReplies: 1Last Post: 11-16-2007, 10:59 AM -
Images not displaying in JSP in IE7
By chadscc in forum Advanced JavaReplies: 0Last Post: 11-13-2007, 03:24 PM


LinkBack URL
About LinkBacks

Bookmarks