• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

printing threads to a text area

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
}
 
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
This will take every ounce of my mental strength! All for a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic