aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Swing and Thread nightmare. Please help. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing and Thread nightmare. Please help." Watch "Swing and Thread nightmare. Please help." New topic
Author

Swing and Thread nightmare. Please help.

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hi,

This has been eating me in and out. I will give the code here you can try it out. Sorry it might be a little long/boring.



I first draw a GUI with a button. This is done by 'GUIDrawer'. I start it as a thread so that EDT will not be blocked. And when you click the 'TEST' button, I want to pop up a frome with YES and NO, and according to user click, the string 'password' will be changed.

The problem is, I want to check the value of 'password' 'after' the user had clicked the button. So I have put in wait()/notify() statements (Thanks to Norm).

But the problem is, no matter what I do or how many threads I start, I cannot get the YES and NO buttons on the passwordFrame. It gives me the frame but not the buttons. If I take off the wait() statement I get the buttons. I'm a litle baffled as to why it gives me the frame but I do not getthe buttons? GUITester is the class associated with the TEST button. Which calls the GetPassword thread and wait(). If you comment the monitor.wait() statement in the GUITester class you can see that the passwordFrame gives the buttons. Otherwise no buttons (only frame).

Any help will be greatly appreciated. Thank you.

[ August 02, 2008: Message edited by: Maduranga Liyanage ]

[ August 02, 2008: Message edited by: Maduranga Liyanage ]
[ August 02, 2008: Message edited by: Maduranga Liyanage ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
This seems a strange (and unnecessary) way to use threads.
I start it as a thread so that EDT will not be blocked.

I don't understand this. Why would the event dispatching thread by blocked?


Ping & DNS - my free Android networking tools app
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Originally posted by Ulf Dittmer:
This seems a strange (and unnecessary) way to use threads.

I don't understand this. Why would the event dispatching thread by blocked?


Thank Ulf.
Could you please let me know what I am doing wrong and why I am not getting the buttons and 'only' the frame?

I 'guessed' wait() blocks the EDT otherwise I cant think of a better reason why I get the frame(with buttons) when there is no monitor.wait() and only the frame when there is monitor.wait().

Thanks mate.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
I don't know what the problem is. To me, there is little point in trying to get this code to work when it seems such a strange way to implement this.

Again: Why are you using threads at all?
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Because if I don't use a thread and wait() on the main, it blocks the JFrame.. atleast thats what I think becasue until I notify() the GUI does not update.

What do you suggest the best way is when I need to 'wait' for user input before proceeding?

Thank you.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
Well, if you didn't use threads, there would be no need to call notify() or wait() or any of the other thread-related methods. So I don't see what could block.

The usual way to trigger an action would be to add an action listener to whatever button the user has to click after he has entered the password.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 686
Here's a working version of your code. One problem was doing things on the EDT in the Test button listener. Another was having more than one object: monitor. I forget the others I found.
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Originally posted by Ulf Dittmer:
Well, if you didn't use threads, there would be no need to call notify() or wait() or any of the other thread-related methods. So I don't see what could block.

The usual way to trigger an action would be to add an action listener to whatever button the user has to click after he has entered the password.


Thanks Ulf.
I'll see if I can work with that. I have put an actionlistener to the button the user if clicking after the password. The problem is the button isnt showing up in the frame when I have wait().
But still dosnt understand why the GUI update is blocking by when I have a wait().
[ August 02, 2008: Message edited by: Maduranga Liyanage ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing and Thread nightmare. Please help.