Reputation: 103
This is my first post here so hopefully it's all okay-like.
I'm in netbeans. I've made a window with buttons and such.
I have a class called SimpleThread that looks like this:
public class SimpleThread extends Thread {
public SimpleThread()
{
}
@Override
public void run()
{
}
And I have different kinds of Subclass threads that extend simplethread (TimerThread, MouseGrabThread).
public class MouseGrabThread extends SimpleThread{
private Point pt;
private JTextField x, y;
public MouseGrabThread(JTextField x, JTextField y)
{
super();
this.x = x;
this.y = y;
}
@Override
public void run()
{
while(this.isAlive())
{
int[] xyVal = new int[2];
xyVal = getCoords();
x.setText("" + xyVal[0]);
y.setText("" + xyVal[1]);
}
}
public int[] getCoords()
{
Point pt = MouseInfo.getPointerInfo().getLocation();
int[] retArr = new int[2];
retArr[0] = (int)pt.getX();
retArr[1] = (int)pt.getY();
return retArr;
}
public class TimerThread extends SimpleThread {
private JTextArea label;
private int time;
public TimerThread(JTextArea label, int time)
{
super();
this.label = label;
this.time = time;
}
@Override
public void run()
{
int counter = time;
while(counter != -1)
{
label.setText("You have: " + counter + " seconds until the timer ends!\n");
counter--;
try {
this.sleep(1000);
} catch (InterruptedException ex) {
System.out.println("Thread Interrupted");
}
}
stop();
}
In my UI Window class, I have this:
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
SimpleThread timer = new TimerThread(jTextArea1, 10); //This counts down from 10 seconds and updates a status text box each second
SimpleThread mouseGrab = new MouseGrabThread(jTextField1, jTextField2); //This grabs mouse coords and updates two boxes in the UI.
timer.start();
if(timer.isAlive())
{
mouseGrab.start();
}
while(timer.isAlive())//######
{
if(!mouseGrab.isAlive())
{
mouseGrab.start();
}
}
}
The program freezes for 10 seconds when I press the button.
I guess the line that I've marked (//#####) is the one thats causing the UI to freeze for the duration of the timer cause it's running in the main thread. I'm not sure how to correct this.
Please excuse my lack of knowledge of programming, I'm just now getting into threads on my own while I'm taking a VERY simple course in University about Data Structures. If possible, can you "dumb" the answer down as much as possible?
Also, I know I'm terrible for doing this, but I call the stop() function even though it's not nice (don't shoot me for it, I don't know how else to do it!) If someone can answer this for me on how to do it nicely, great!
Upvotes: 1
Views: 117
Reputation: 4216
What you might want is ending mouseGrab
at the end of the countdown:
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
SimpleThread timer = new TimerThread(jTextArea1, 10); //This counts down from 10 seconds and updates a status text box each second
SimpleThread mouseGrab = new MouseGrabThread(jTextField1, jTextField2); //This grabs mouse coords and updates two boxes in the UI.
mouseGrab.start();
timer.start();
// Wait until countdown finishes
while(timer.isAlive()) {}
mouseGrab.stop();
}
In the code you pasted, you kept starting your mouseGrab
while timer
was running. You might rather want to let mouse grabbing running while the timer is on.
Edit : indeed, stop()
is deprecated, you really should use a boolean running
attribute in your TimerThread
and wrapping the content of its run()
method in some
while (running) {
/* stuff */
}
and then "stop" this thread externally with some setter. The correct answer would then be for example:
mouseGrab.start();
timer.start();
// Wait until countdown finishes
while(timer.isAlive()) {}
mouseGrab.setRunning(false);
}
Edit2 : this eventually seems to be what you want:
private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {
SimpleThread mouseGrab = new MouseGrabThread(jTextField1, jTextField2); //This grabs mouse coords and updates two boxes in the UI.
SimpleThread timer = new TimerThread(mouseGrab, jTextArea1, 10); //This counts down from 10 seconds and updates a status text box each second
mouseGrab.start();
timer.start();
}
With:
public class MouseGrabThread extends SimpleThread {
private Point pt;
private JTextField x, y;
private boolean running;
public MouseGrabThread(JTextField x, JTextField y) {
super();
this.x = x;
this.y = y;
}
@Override
public void run() {
running = true;
while(running) {
int[] xyVal = new int[2];
xyVal = getCoords();
x.setText("" + xyVal[0]);
y.setText("" + xyVal[1]);
}
}
public int[] getCoords() {
Point pt = MouseInfo.getPointerInfo().getLocation();
int[] retArr = new int[2];
retArr[0] = (int)pt.getX();
retArr[1] = (int)pt.getY();
return retArr;
}
public void break() {
this.running = false;
}
}
// ------------- //
public class TimerThread extends SimpleThread {
private MouseGrabThread mouseGrab;
private JTextArea label;
private int time;
public TimerThread(MouseGrabThread mouseGrab, JTextArea label, int time)
{
super();
this.label = label;
this.time = time;
this.mouseGrab = mouseGrab;
}
@Override
public void run()
{
int counter = time;
while(counter != -1)
{
label.setText("You have: " + counter + " seconds until the timer ends!\n");
counter--;
try {
this.sleep(1000);
} catch (InterruptedException ex) {
System.out.println("Thread Interrupted");
}
}
mouseGrab.break();
}
}
Upvotes: 1