Results 1 to 4 of 4
- 04-28-2011, 05:21 PM #1Member
- Join Date
- Apr 2011
- Rep Power
Blocking prevents JFrame from being fully drawn.
I have a problem that I need help with. I initially thought it was a thread problem, but now I believe it is a Swing problem. I am writing an 'int chooser'. The goal was to have it behave like a file chooser - a small JFrame opens and presents OK and CANCEL buttons, as well as a JSlider with feedback (JTextfield).
Here are the methods of interest:
- Constructor - sets member variables
- showChooserFrame - does all the swing GUI creation and sets the frame visible
- run (this is a thread) - calls showChooserFrame using invokeLater. I have tried calling this from the contructor, as well as the client creating the object.
- getValue - returns the value selected by the user
- exitWindow - dispose of frame and cleanup
IntChooser ic = new IntChooser(<parameters>);
int theInt = ic.getValue();
Therefore, I need blocking somewhere while the user selects a value. Therein lies my problem. I have tried to block using many methods in several places, and every time I do, the JFrame is not completely created - it is merely a frame with a title and nothing inside. As soon as I remove blocking, the frame paints just fine, but the value is returned well before a user has a chance to do anything.
I have tried blocking at the end of the run method using a (binary) semaphore as well as a while(!userDone) loop. I have tried blocking at the beginning of the getValue method using a synchronized lock. I changed the getValue to a 'callable'. None have worked.
My exitWindow routine is called when any button is pressed or the window is closed (by the X). This is where I typically have unblocked (released semaphore, set userDone to true, etc). But that is moot really, since the frame is never fully revealed.
Any help is appreciated.
- 04-28-2011, 05:28 PM #2
To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
Don't forget to use the code tags so that your code retains its formatting.
Oh and you might want to go through Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)
- 04-28-2011, 05:29 PM #3
- 04-28-2011, 10:43 PM #4Member
- Join Date
- Apr 2011
- Rep Power
I would have posted a snippet of code, but the machine I work on is not (and cannot be) on the public internet. And transferring data is not trivial. I will try to set something up next week if I don't have this solved by then.
On the Java Tutorials you mentioned: they leave much to be desired. I have read many of them, and for simple to moderate concepts they are fine. They lack detail on more complex concepts IMO. I prefer published books by professionals, but mine are all outdated (J2SE 1.4). I am seeking updated versions/replacements.
Modal dialog - yes, I had forgotten about those. Good point and thanks for the reminder.
The latest status - I was cleaning up the code (it has been through several 'refactoring' sessions) and decided to call my class from main. It worked fine! Ouch. So it appears the problem is in the main JFrame. The main JFrame has menus, and my new class is called from there (via actionPerformed).
Lesson learned: just because an app works (the main JFrame had no problems before - but there was no blocking before), doesn't mean it is correct. :o
- By hello2010 in forum AWT / SwingReplies: 7Last Post: 03-18-2011, 06:28 AM
- By renu in forum New To JavaReplies: 14Last Post: 09-21-2010, 05:10 PM
- By jdrury in forum Jobs OfferedReplies: 1Last Post: 08-11-2009, 04:19 PM
- By jdrury in forum Jobs OfferedReplies: 2Last Post: 08-11-2009, 03:58 PM
- By Cold-Gin in forum EclipseReplies: 1Last Post: 04-09-2009, 11:49 PM