File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "printing threads to a text area" Watch "printing threads to a text area" New topic

printing threads to a text area

MaryEllen Volb
Ranch Hand

Joined: Jan 31, 2001
Posts: 35
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();

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
//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
//Action event handler
// Create threads
PrintChar printA = new PrintChar('a', 100);
PrintChar printB = new PrintChar('b', 100);
PrintNum print100 = new PrintNum(100);
// Start threads
public void actionPerformed(ActionEvent e)
// 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++)
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
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.
I agree. Here's the link:
subject: printing threads to a text area
It's not a secret anymore!