wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes printing threads to a text area Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "printing threads to a text area" Watch "printing threads to a text area" New topic
Author

printing threads to a text area

MaryEllen Volb
Ranch Hand

Joined: Jan 31, 2001
Posts: 35
Hello,
I have to print three threads simultaneously to a text area. I think I have what I need in this program, but I am still getting compiler errors. Any suggestions?
Thanks, Mary Ellen
package Chapter13;
import Chapter8.MyFrameWithExitHandling;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PrintThreadsToTextArea extends MyFrameWithExitHandling
implements ActionListener
{
private JTextArea jta;
//public StringBuffer buf = new StringBuffer();
public JButton jbt;

// Main method
public static void main(String[] args)
{
PrintThreadsToTextArea frame = new PrintThreadsToTextArea();
frame.pack();
frame.center();
frame.setSize(400,300);
frame.setVisible(true);
}

//Constructor
public PrintThreadsToTextArea()
{
setTitle("Print Threads to Text Area");
//Create panel p to hold the tesxt field
JPanel p = new JPanel();
p.setLayout(new FlowLayout());
p.add(jta = new JTextArea());
//Create scroll pane to hold text area
JScrollPane scrollPane = new JScrollPane(jta = new JTextArea());
//set line wrap
jta.setLineWrap(true);
//set FlowLayout for the frame and add components to it
getContentPane().setLayout(new BorderLayout());
getContentPane().add(scrollPane, BorderLayout.CENTER);
getContentPane().add(p, BorderLayout.SOUTH);
//register listeners
jta.addActionListener(this);
jbt.addActionListener(this);
}
//Action event handler
// Create threads
PrintChar printA = new PrintChar('a', 100);
PrintChar printB = new PrintChar('b', 100);
PrintNum print100 = new PrintNum(100);
// Start threads
print100.start();
printA.start();
printB.start();
public void actionPerformed(ActionEvent e)
{
jta.append(jta.getText().trim());
}
// The thread class for printing a specified character
// in specified times
class PrintChar extends Thread
{
private char charToPrint;
private int times;
public PrintChar(char c, int t)
{
charToPrint = c;
times = t;
}
// Override the run() method to tell the system
// what the thread will do
public void run()
{
for (int i=1; i < times; i++)
System.out.print(charToPrint);
}
}
class PrintNum extends Thread
{
private int lastNum;

public PrintNum(int n)
{
lastNum = n;
}
public void run()
{
for (int i=1; i <= lastNum; i++)
System.out.print(" " + i);
}
}
}
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
MaryEllen: What compile errors are you getting?
See that we can�t compile your code because there are missing classes like: MyFrameWithExitHandling but perhaps if you post the compile errors you are getting we can help you.
MaryEllen Volb
Ranch Hand

Joined: Jan 31, 2001
Posts: 35
HI,
the compiler error I got was "method does not return a value;return type required" where the threads start in the program. If you can help at all, I'd appreciate it!!!
Thanks, Mary Ellen
Matt Siegel
Ranch Hand

Joined: Jul 18, 2000
Posts: 55
The start() calls are not being made from inside any method. I think that your brackets are messed up. Go back through your code again.
Matt
 
 
subject: printing threads to a text area
 
Similar Threads
Edit cell with another JTable
Simple question regarding runnable interface
font size and JDialog box
how to set the refresh() method for JTextarea ???
using thread class to print to a text area