I am having problems setting the text inside of a JTextArea. My Program is using 3 threads to print onto the JTextArea, using increments. I can print onto the command prompt using System.out.println with no problem, but for some reason I can't print to the JTextArea. I am brand new to this website, so please let me know if you need anymore additional information! Thanks for any help!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
class SimpleFrame extends JFrame
{
public static final int HEIGHT = 500;
public static final int WIDTH = 600;
public static JTextArea jta;
public SimpleFrame()
{
setSize(WIDTH, HEIGHT);
setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
final JButton tim = new JButton("Tim (5 sec)");
final JButton suzy = new JButton("Suzy (3 sec)");
final JButton edna = new JButton("Edna (2 sec)");
topPanel.add(tim);
topPanel.add(suzy);
topPanel.add(edna);
add(topPanel, BorderLayout.NORTH);
JPanel textPanel = new JPanel();
jta = new JTextArea(10,10);
jta.setFont(new Font("SansSerif", Font.PLAIN, 20));
add(new JScrollPane(jta), BorderLayout.CENTER);
tim.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Runnable r1 = new myRunnable(5, "Tim", "Tim", 5000);
Thread t1 = new Thread(r1);
t1.start();
tim.setEnabled(false);
}
});
suzy.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Runnable r2 = new myRunnable(3, "Suzy", "Suzy", 3000);
Thread t2 = new Thread(r2);
t2.start();
suzy.setEnabled(false);
}
});
edna.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Runnable r3 = new myRunnable(1, "Edna", "Edna", 1000);
Thread t3 = new Thread(r3);
t3.start();
edna.setEnabled(false);
}
});
}
}
class myRunnable extends SimpleFrame implements Runnable
{
int workerTime;
String name;
String threadToRun;
int runtimeDelay;
public myRunnable(int time, String workerName, String thread, int delay)
{
workerTime = time;
name = workerName;
threadToRun = thread;
runtimeDelay = delay;
}
public void run()
{
int i = 0;
while(i < 10)
{
try
{
jta.append(name + " is working, count = " + workerTime + "\n");
workerTime += workerTime;
Thread.sleep(runtimeDelay);
}
catch(InterruptedException e)
{
System.out.println("Error: " + e);
}
}
}
}
public class Names
{
public static void main(String[] args)
{
SimpleFrame frame = new SimpleFrame();
frame.setTitle("Multi Thread Workers");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
The JTextArea should be printing something similar to this:
Tim is working, count = 0
Tim is working, count = 1
Suzy is working, count = 0
Suzy is working, count = 1
Edna is working, count = 0
Tim is working, count = 2
Edna is working, count = 1
Suzy is working, count = 2
Edna is working, count = 2
Tim is working, count = 3
Suzy is working, count = 3
Edna is working, count = 3
Here is a snip-it of the code I'm having a problem with:
jta.append(name + " is working, count = " + workerTime + "\n");
workerTime += workerTime;
Thread.sleep(runtimeDelay);
It complies just fine.. I just don't get the output-- that's why I think it's something super simple that I'm just missing.
HERE IS MY NEW CODE: I am getting the "cannot find symbol" errors for frame now inside of the actionlisteners and "cannot find symbol" on jta.append
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Names
{
public static void main(String[] args)
{
SimpleFrame frame = new SimpleFrame();
frame.setTitle("Multi Thread Workers");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class SimpleFrame extends JFrame
{
public static final int HEIGHT = 500;
public static final int WIDTH = 600;
public static JTextArea jta;
public SimpleFrame()
{
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
setVisible(true);
JPanel topPanel = new JPanel();
final JButton tim = new JButton("Tim (5 sec)");
final JButton suzy = new JButton("Suzy (3 sec)");
final JButton edna = new JButton("Edna (2 sec)");
topPanel.add(tim);
topPanel.add(suzy);
topPanel.add(edna);
add(topPanel, BorderLayout.NORTH);
JPanel textPanel = new JPanel();
jta = new JTextArea(10,10);
jta.setFont(new Font("SansSerif", Font.PLAIN, 20));
add(new JScrollPane(jta), BorderLayout.CENTER);
tim.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Runnable r1 = new myRunnable(5, "Tim", "Tim", 5000, frame, jta);
Thread t1 = new Thread(r1);
t1.start();
tim.setEnabled(false);
}
});
suzy.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Runnable r2 = new myRunnable(3, "Suzy", "Suzy", 3000, frame, jta);
Thread t2 = new Thread(r2);
t2.start();
suzy.setEnabled(false);
}
});
edna.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Runnable r3 = new myRunnable(1, "Edna", "Edna", 1000, frame, jta);
Thread t3 = new Thread(r3);
t3.start();
edna.setEnabled(false);
}
});
}
}
class myRunnable implements Runnable
{
int workerTime;
String name;
String threadToRun;
int runtimeDelay;
JFrame frame;
JTextArea jTextArea;
public myRunnable(int time, String workerName, String thread, int delay, JFrame f, JTextArea j)
{
workerTime = time;
name = workerName;
threadToRun = thread;
runtimeDelay = delay;
frame = f;
jTextArea = j;
}
public void run()
{
int i = 0;
while(i < 10)
{
try
{
jta.append(name + " is working, count = " + workerTime);
workerTime += workerTime;
frame.repaint();
Thread.sleep(runtimeDelay);
}
catch(InterruptedException e)
{
System.out.println("Error: " + e);
}
}
}
}
Here is your code edited so that it works as you would like.
I will include a list of changes at the bottom
public class Names {
public static void main(String[] args) {
SimpleFrame frame = new SimpleFrame();
frame.setTitle("Multi Thread Workers");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class SimpleFrame extends JFrame {
public static final int HEIGHT = 500;
public static final int WIDTH = 600;
public JTextArea jta;
public SimpleFrame() {
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel topPanel = new JPanel();
final JButton tim = new JButton("Tim (5 sec)");
final JButton suzy = new JButton("Suzy (3 sec)");
final JButton edna = new JButton("Edna (2 sec)");
topPanel.add(tim);
topPanel.add(suzy);
topPanel.add(edna);
add(topPanel, BorderLayout.NORTH);
JPanel textPanel = new JPanel();
jta = new JTextArea(10, 10);
jta.setFont(new Font("SansSerif", Font.PLAIN, 20));
JScrollPane jsp = new JScrollPane(jta);
add(jsp, BorderLayout.CENTER);
tim.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Runnable r1 = new myRunnable(5, "Tim", "Tim", 5000,
SimpleFrame.this, jta);
Thread t1 = new Thread(r1);
t1.start();
tim.setEnabled(false);
}
});
suzy.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Runnable r2 = new myRunnable(3, "Suzy", "Suzy", 3000,
SimpleFrame.this, jta);
Thread t2 = new Thread(r2);
t2.start();
suzy.setEnabled(false);
}
});
edna.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
Runnable r3 = new myRunnable(1, "Edna", "Edna", 1000,
SimpleFrame.this, jta);
Thread t3 = new Thread(r3);
t3.start();
edna.setEnabled(false);
}
});
jta.setVisible(true);
jsp.setVisible(true);
topPanel.setVisible(true);
setVisible(true);
}
}
class myRunnable implements Runnable {
int workerTime;
String name;
String threadToRun;
int runtimeDelay;
JFrame frame;
JTextArea jTextArea;
public myRunnable(int time, String workerName, String thread, int delay,
JFrame f, JTextArea j) {
workerTime = time;
name = workerName;
threadToRun = thread;
runtimeDelay = delay;
frame = f;
jTextArea = j;
}
public void run() {
int i = 0;
while (i < 10) {
try {
jTextArea.append(name + " is working, count = " + workerTime);
workerTime += workerTime;
frame.repaint();
Thread.sleep(runtimeDelay);
} catch (InterruptedException e) {
System.out.println("Error: " + e);
}
}
}
}
Changes:
SimpleFrame.this
, as you want to give each thread an instance of the jframe they need to use. the variable frame was only defined within the main method and thats why you couldn't use it in the SimpleFrame class.
jta.setVisible(true);
jsp.setVisible(true);
topPanel.setVisible(true);
setVisible(true);
static
from jta in its declaration in SimpleFrame. you don't want this to be the same for every SimpleFrame.If you have anymore questions you can email me (my email is visible from my profile) Since your new to the site, you should accept the answer by clicking on the check mark because this closes the question.