aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JDialog and WindowFocusListener Not working the way I think it should. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JDialog and WindowFocusListener Not working the way I think it should." Watch "JDialog and WindowFocusListener Not working the way I think it should." New topic
Author

JDialog and WindowFocusListener Not working the way I think it should.

joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
Below is the code that I'm working with.

I am creating (attempting) button that pops up a calendar allowing the choosing of a date. All this code works fine. however, someone suggest I use a Popup window. In my research, I came across some examples to use WindowFocusListener. So I said GREAT! I began to add this code to mine. Well it works, some what. When I have the JDialog.setModal( false), it hides the window when I click anywhere other then the calDialog() but it wont return the date; But if I set the JDialog.setModal(True); it returns the date but will not close the window when clicking outside the window (PULLING ON HAIR) I'm so close, I can feel it...

What the heck am I missing?


This is the code snippet that calls my class



This is the class that I'm working on

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
There is not need for the invokeLater() in you WindowListener. All event code already executes on the EDT.

If you need more help then post an SSCCE that demonstrates the problem.
joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
Ok, thanks for the help, this didn't fix the problem with returning the date. Still acts the same way as my above post with that removed.

I modified the above code example to reflect the removal of the invokelater.
joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
I was thinking about why the above code doesn't act well when I setModal(false) because I think the calling code moves on, with nothing actually happening? When I setModal(true) it waits for return before moving on? So I guess to modify my question above, do I need to implement some code in the calling button event to halt til the dialog closes? This is very foggy area in java for me.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
I think the calling code moves on, with nothing actually happening? When I setModal(true) it waits for return before moving on?


Sorry, yes that is the real problem.

I guess the queston is why are you trying to make the dialog non-modal?

Generally the way a modal dialog works is that you dsplay the dialog and wait for the user to do something with the dialog. If the user selects "Ok" then you save the data the user changed. If the user selects "Cancel" then you don't do anything.

If you use a non-model dialog then clicking outside the dialog would be similiar to clicking on the "Cancel" button. That is you should close the dialog without saving the changes. So I would say your current behaviour is correct.
joe vasher
Ranch Hand

Joined: Nov 06, 2009
Posts: 37
Thanks again for your help.

I'm teaching myself programming, its probably obvious by these questions and my programming style. So that is why I'm not sure about some of the uses.

I originally wrote the program to use a dialog. Then one of the replies to a earlier question on here, was that I probably wanted to use a Popup window, so I did research on it. a feature of the popup window I like, is that it would go away if the user clicked else where in the window, really wanted to add this feature to my calendar chooser. Please keep in mind I'm trying to learn java and its more advance features. So I have to say I didn't realize you couldn't use the windowlostfocus with a dialog and have it do what I wanted.

So with above code, What direction would you suggest I continue my research into adding the windowlostfocus (so the calendar "Window" that pops up goes away?)
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2203
    
    7
Then one of the replies to a earlier question on here, was that I probably wanted to use a Popup window,


Replies are only as good as the question. If all the requirements aren't given in the question then its hard to give an exact solution.

Having a popup appear an disappear isn't really a requirement. How the popup works is the requiremennt then you design your solution.

For example, the current design of using a modal dialog is easy because the user must explicitly indicate when they are finished with the dialog by clicking on an "Ok" or "Cancel" button. When the user clicks "Ok", then you update the text field with the selected date. When the user clicks "Cancel" you don't update the text field. Right now your code always updates the date even if the user cancels the dialog, which is a problem that should be fixed.

The second approach, when you don't use a modal dialog becomes a little more complicated. As I tried to say earlier, it is not a good idea to automatically update the text field just because the window loses focus. There can be many reasons for a window closing. For example you may switch to another window to do something. In this case the wiindow will close even though you may not have finished choosing your date. Now you text field contains invalid data. There are a couple of different solutions.

First you should not update the text field on a closing of the window. Instead you pass the text field as a parameter to the day chooser. Then when you user selects a day (either by double clicking on the day, or by using the Enter key), you update the text field with the day and then close the window.

A second approach might be to constantly update the text field every time you select a new day (either by clicking on a day or by tabbing from day to day). Again the closing of the window would do nothing since the text field has already been updated. Now the difference for this approach is that you would need to save the original day so that you can restore the text field to its original value in case the user uses the Escape key which is meant to back out any changes to the text field. Approaches like this would typically use a PropertyChangeListener to notify a class every time a change is made so that you can update your text field.

I suggest you check out http://www.toedter.com/en/jcalendar/. It has a simple day chooser which I believe uses PropertyChangeListeners (although I haven't looked at the code in 8 years so it may have changed).

What I'm really trying to say is that the windowLostFocus() is just used to close the window. It is not use to set the data.
 
 
subject: JDialog and WindowFocusListener Not working the way I think it should.