Results 1 to 4 of 4
  1. #1
    Bart is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default 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
    I want the client call to look something like:
    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.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    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)

    db

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    Also, it rather looks like you might be wanting a modal dialog.
    How to Make Dialogs (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)

    db

  4. #4
    Bart is offline Member
    Join Date
    Apr 2011
    Posts
    3
    Rep Power
    0

    Default

    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

Similar Threads

  1. Replies: 7
    Last Post: 03-18-2011, 06:28 AM
  2. Replies: 14
    Last Post: 09-21-2010, 05:10 PM
  3. Replies: 1
    Last Post: 08-11-2009, 04:19 PM
  4. Replies: 2
    Last Post: 08-11-2009, 03:58 PM
  5. Catch 22: Ant prevents CVS commit
    By Cold-Gin in forum Eclipse
    Replies: 1
    Last Post: 04-09-2009, 11:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •