textarea can't display

while(f1.exists())

try{

Thread.sleep(1000);

}

catch (Exception x){

}

if (!f1.exists()){

ta.append("USB diskdrive has been removed!\n\n");

}

while(!f1.exists())

try{

Thread.sleep(1000);

}

catch (Exception x){

}

if (f1.exists()){

ta.append("USB diskdrive inserted!\n\n");

}

when USB drive is removed or insert the textarea will display "USB removed" or "USB inserted" but why it only happen for one time, meaning i can't enable the textarea to display each time i remove and insert the USB drive? Hope to have assistance thanks alot.

jp

[1288 byte] By [juniorprogrammera] at [2007-11-27 9:04:29]
# 1
It only happens one time because it's not in any kind of loop.
paulcwa at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 2

> while(f1.exists())

> try {

> Thread.sleep(1000);

> }

> catch (Exception x) {

> }

> if (!f1.exists()) {

> ta.append("USB diskdrive has been removed!\n\n");

> }

>

> while(!f1.exists())

> try {

> Thread.sleep(1000);

> }

> catch (Exception x) {

> }

> if (f1.exists()) {

> ta.append("USB diskdrive inserted!\n\n");

> }

when USB drive is removed or insert the

> textarea will display "USB removed" or "USB inserted"

> but why it only happen for one time, meaning i can't

> enable the textarea to display each time i remove and

> insert the USB drive? Hope to have assistance thanks

> alot.

>

> jp

This seems almost too much like this thread:

http://forum.java.sun.com/thread.jspa?threadID=5189456&tstart=0

I mean, c'mon... how many people ask about displaying messages regarding the status of their USB drive in one night ?

Navy_Codera at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 3
yup i can't create a loop because it is all in one method together with the GUI, is there a way to loop the action above?thanksjp
juniorprogrammera at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 4
? poll it w/ a timer?
petes1234a at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 5

Timer t = new Timer(500,this);

while(f1.exists()||f2.exists()||f3.exists())

try {

Thread.sleep(1000);

}

catch (Exception x) {

}

if (!f1.exists()&&!f2.exists()&&!f3.exists()) {

ta.append("USB diskdrive has been removed!\n\n");

}

while(!f1.exists()&&!f2.exists()&&!f3.exists())

try {

Thread.sleep(1000);

}

catch (Exception x) {

}

if (f1.exists()||f2.exists()||f3.exists()) {

ta.append("USB diskdrive inserted!\n\n");

}

t.start();

public static void main (String[] args) {

t.start();

}

i can't reloop the thread, can anyone help? :(

thanks

jp

juniorprogrammera at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 6

What you have there makes absolutely no sense.

Maybe something more like:

Timer t = new Timer();

t.schedule(new TimerTask() {

private boolean exists = false;

public void run() {

if (f1.exists() ^ exists) {

System.out.println("USB drive " + (f1.exists() ? "inserted" : "removed"))

exists = f1.exists();

}

}

});

This is entirely untested, and in fact I know there are errors there, but it seems like the way to go. Read the manual for details on how to correct it.

Also, the premise that you can use File.exists() to check whether a USB drive has been inserted, seems dubious. Double-check that.

paulcwa at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 7

To simply answer your question ... you should put all that code into a loop!

The best way to implement this would be to create a seperate thread (so that you can still use the main app while it is running) ... here is some info on threads:

http://java.sun.com/j2se/1.3/docs/api/java/lang/Thread.html

In the constructor check the state of the USB drive and record that value (bool). Then instead of writing some text every time, just check if the state of the drive has changed, and raise an event when it changes ;-)

nippysaurusa at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 8

import java.util.Timer;

import java.util.TimerTask;

Timer t = new Timer();

t.schedule(new TimerTask() {

private boolean exists = false;

public void run() {

if (f1.exists() ^ exists) {

System.out.println("USB drive " + (f1.exists() ? "inserted" : "removed"))

exists = f1.exists();

}

}

});

Thanks for the help above when I compiled i get this, cannot find symbol method schedule(<anonymous java.util.TimerTask>) what is wrong?

thank you

jp

juniorprogrammera at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 9

Like I said, the code has errors. There is no such method "schedule" that takes a single TimerTask argument. Rather, there are multiple "schedule" methods that each take multiple arguments, plus some "scheduleAtFixedRate" methods that also take multiple arguments. I just put "schedule(TimerTask)" in there because I didn't know which version you needed and just wanted to get the point across.

Read the help page in the API documentation about java.util.Timer to see what the various different methods do, and to figure out which one you need, and how to invoke it.

paulcwa at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 10
well, you were told that the code was untested, and so believe it. seems like you need to do some work: look at the api and find out what's missing, then correct it. good luck.
petes1234a at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 11

It's example code, not finished code. I posted it as an example of how you can get the functionality you seem to need, in a simpler and more reliable way; I didn't post it to do your work for you. You should read it, look up the parts you don't understand (or which don't compile), learn what it does and how, and adjust it as needed.

paulcwa at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 12

I thought of doing it in thread form whereby i call back the loop when the USB is inserted and remove. The problem is i need to display the message "Drive inserted" or "Drive removed" in this form ta.append(....) instead of System.out.println(.....). When I compiled it says cannot find variable ta.

How do i want to separate all the actions in one method and the GUI itself in another method? Hope anyone can help me. Thank you.

jp

juniorprogrammera at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 13

If you make the TimerTask an anonymous inner class, you can invoke fields or final local variables of the class or method it's called from.

For example:

public MyClass() { // constructor

Frame f = new Frame();

final TextArea ta = new TextArea();

Timer tmr = new Timer();

tmr.schedule(new TimerTask() {

private boolean ticktock;

public void run() {

ta.append( ticktock ? "Tick" : "Tock" );

ticktock = !ticktock;

}

});

// add textarea to the frame, display it, etc.

}

paulcwa at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...
# 14
thanks for the guide :)
juniorprogrammera at 2007-7-12 21:37:43 > top of Java-index,Java Essentials,New To Java...