|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-27-2008, 04:34 PM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 20
|
|
|
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.
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, 07:29 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
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.
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-27-2008, 07:55 PM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 20
|
|
|
how do you mean sukatoa?
|
|

04-27-2008, 08:17 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
How? The code? ok.....
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....
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-27-2008, 09:12 PM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 20
|
|
|
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, 09:40 PM
|
 |
Senior Member
|
|
Join Date: Jan 2008
Location: Cebu City, Philippines
Posts: 527
|
|
|
Yah... my idea is conflict to your design....
Have you check the output in console?
__________________
A specific, detailed, simple, well elaborated, and "tested before asking" question may gather more quick replies. hopefully To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-27-2008, 10:37 PM
|
|
Member
|
|
Join Date: Aug 2007
Posts: 20
|
|
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:
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
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);
}
}
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|