Why windowclosing event occurs before focusLost event of JTextField?

I have the following code:
JTextField field = new JTextField();
*field.addFocusListener(new FocusAdapter(){*
*public void focusLost(FocusEvent e) {*
JOptionPane.showMessage(null,"test");
*window.addWindowListener(new WindowAdapter() {*
*public void windowClosing(final WindowEvent e) {*
window.dispose();
when the focus in field, then I click X button on window, window is closed, but then message dialog shows.
I want message dialog must show before window closing.
Help me.
thanks very much.
Edited by: phamtrungkien on Sep 24, 2008 2:23 AM

You could try wrapping the call to dispose() in a SwingUtilities.invokeLater.
Note that the Swing/AWT event model does not guarantee the order of delivery of events, so the sequence of execution of listener methods is not an accurate indicator of the order in which the events were generated.
db
edit What class is your variable reference window?
If JFrame, what defaultCloseOperation is set?
Edited by: Darryl.Burke

Similar Messages

  • Events That Will Occur Before the 2.0 Upgrade Works for Everyone

    1. Not only will Chinese Democracy by Guns N' Roses will be released, but there will actually be Democracy in China.
    Go.
    -Dominique
    P.S. How bored am I?
    EDIT: Oh, cool. It works. Nice timing, eh?

    Events That Will Occur Before the 2.0 Upgrade Installs on My Computer
    1. Dakota Fanning will die of old age.
    -Dominique
    P.S. Yeah. I'm awesome at recovery.

  • How to trap WindowClose event?

    I would like to trap the WindowClose event such that when my closing condition is not satisfied, the window would not close.
    Right now everytime i click the X button at the upper right of the window, the window closes automatically. I would like to know how to trap that event so I can control the closing of the window.
    tnx!

    For that you have to implement java.awt.event.WindowListener and override the windowClosing or windowClosed method according to your requirement. Put your code inside that method. But before doing that you have to set the default close operation.
    setDefaultCloseOperation(0);
    Hope this would work.
    Anirban

  • Calling a JDialog in the windowClosing Event of a JFrame

    hi
    As we had seen in many applications when u try to close the application a Dialog box will appear and it will ask whether we have to save it.I want to implement the same thing in my windowClosing Event of the Frame.I tried doing this by calling the Dialog i have created by using the show method in the window closing event of the Frame,but what happens is when i close the frame the Dialog box flickers and disappears.What should i do to avoid this flickering and Disppearing of the Dialog box.I want the Dialog box to stay on the screen.What should i do? any ideas?
    thanks

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class WindowClosingTest extends JFrame {
         public WindowClosingTest() {
              try {
                   setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
                   JButton button = new JButton("Close");
                   button.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent ae) { performClose(); }
                   addWindowListener(new WindowAdapter() {
                        public void windowClosing(WindowEvent we) { performClose(); }
                   getContentPane().add(button);
                   pack();
                   setLocationRelativeTo(null);
                   setVisible(true);
              } catch (Exception e) { e.printStackTrace(); }
         private void performClose() {
              int n = JOptionPane.showConfirmDialog(null, "Close?", "Confirm", JOptionPane.YES_NO_OPTION);
              if (n != 1) { System.exit(0); }
         public static void main(String[] args) { new WindowClosingTest(); }
    }

  • Stopping WindowClosing Event

    HI, I have a question about stopping WindowClosing Event from continuing to WindowClosed Event.
    I have a JFrame implementing WindowListener - and I'd like to stop frame from closing by intercepting WindowClosing Event (so it never reaches WindowClosed Event).
    Is there anyone who know how to do it?
    Thank you in advance.

    Once you set the default close operation as follows:
    setDefaultCloseOperation( DO_NOTHING_ON_CLOSE );
    You must explicitly close the window yourself. Something like:
    addWindowListener( new WindowAdapter()
         public void windowClosing(WindowEvent e)
              String message = "Do you want to close the window?";
              int result = JOptionPane.showConfirmDialog(parent, message, "Close Window", JOptionPane.YES_NO_OPTION);
              if (result == JOptionPane.YES_OPTION)
                   window.dispose();
    });

  • Why different events in iPhoto and in iPhone when i synchronize all events in my Mac

    why different events in iPhoto and in iPhone when i synchronize all events in my Mac?

    Because there is no connection betind iPhoto on the Mac and iPhoto for IOS other than  the name
    LN

  • Help !! needed with windowClosing() event

    Hi there I have a problem were I would like to shut down my application when it is iconified,
    THE SHORT STORY
    I would like my application to be deiconified when I close it by right click in its icon and select the close option.
    BACKGROUND
    the current situation is when it is iconified I can right click and then I get the menu where I can choose to restore or close, when I click close, a windowclosing event is fired but nothing happens the application is not shut shown, when I then right click again and restore the app I notice that my own built in logout menu is up so it seems to be recieving the event all I want is the appp to be deiconified when I clik close.
    As I am a distributed programmer this gui stuff is rocket science i would appreciate any help

    Something like the following, though I am not sure too.
         win.addWindowListener(new WindowAdapter() {
              public void windowClosing(WindowEvent e) {
                   // Invoked when a window is in the process of being closed. The close operation can be overridden at this point.
                   Window win = e.getWindow();
                   boolean iconified = win.getState() == ICONIFIED;
                   if (iconified) {
                        e.consume();
                        win.setState(NORMAL);
                   } else {
                        super.windowClosing(e);

  • JDialog dispose() not firing windowClosing()/windowClosed() event

    I have a small dialog that has 'OK' and 'CANCEL' buttons. When the user clicks on the 'X' in the top right hand corner to close the window, the windowClosing() event is fired on all listeners. However, calling dipose() when i click on the 'OK' or 'Cancel' buttons does not fire the event. I am sure this should work without a problem but i just cannot see what is going wrong.
    Andrew

    I'd have to test this, but there's some sort of logic to think that calling hide or dispose would not generate events. What would be the point? You know it's going to be closed, so you should tell anyone who needs to know. You can get the list of listeners and fire your own event if you wanted.
    Window.dispose() does fire a window closed event, though.
    If you want to simulate closing... This is from code I had written... some of it you can replace as appropriate (isJFrame(), etc).
          * Closes the window based on the window's default close operation. 
          * The reason for this method is, instead of just making the window
          * invisible or disposing it, to rely on either the default close
          * operation (for JFrames or JDialogs) or rely on the window's other
          * listeners to do whatever the application should do on the "window
          * closing" event. 
         private void doClose() {
              int closeOp = getDefaultCloseOperation();
              // send the window listeners a "window closing" event... 
              WindowEvent we = new WindowEvent(getWindow(), WindowEvent.WINDOW_CLOSING, null, 0, 0);
              WindowListener[] wl = getWindow().getWindowListeners();
              for(int i = 0; i < wl.length; i++) {
                   // this handler doesn't need to know...
                   if(wl[i] == this) {
                        continue;
                   wl.windowClosing(we);
              // if still visible, make it not (maybe)...
              if(getWindow().isVisible()) {
                   switch(closeOp) {
                        case WindowConstants.HIDE_ON_CLOSE:
                             getWindow().setVisible(false);
                             break;
                        case JFrame.EXIT_ON_CLOSE:
                        case WindowConstants.DISPOSE_ON_CLOSE:
                             getWindow().setVisible(false);
                             getWindow().dispose();
                             break;
                        case WindowConstants.DO_NOTHING_ON_CLOSE:
                        default:
         * Gets the default close operation of the frame or dialog.
         * @return the default close operation
         public int getDefaultCloseOperation() {
              if(isJFrame()) {
                   return ((JFrame)getWindow()).getDefaultCloseOperation();
              if(isJDialog()) {
                   return ((JDialog)getWindow()).getDefaultCloseOperation();
              // "do nothing" is, for all intents and purposes, the way AWT
              // Frame and Dialog work.
              return WindowConstants.DO_NOTHING_ON_CLOSE;

  • Why the event latch: cache buffers chains wait event arises and resolution

    Can any one please give full information about:
    latch: cache buffers chains wait event
    Why this event arises and resolution?

    Google gave me
    http://www.pythian.com/news/1135/tuning-latch-contention-cache-buffers-chain-latches/

  • Why do duplicates occur in iTunes and how to avoid them

    Why do duplicates occur in iTunes. I just moved my Library to a external hard drive to clear up space on my HD. Now I have dups. Sometime 3 of each song, sometimes 6 of each song. All together I ended up with more that 4,000 dups.
    I have spend more time researching how to get rid of dups,( without manually taking them out of my itunes library), that the time it took to just manually remove them. I did download and intall the icorraldupes script, but that only fines the dups. If you delete them from the dup playlist they are still in the Library. A good script for reference, though. So, how do I avoid dups in the future? I just finished cleaning up my Libary of all dups. I figured it must have been something I did and would not want to repeat it again. I would hate to think that this is just some kind of bug or quirk within the iTunes program. If so I would not want to trust my collection of music in iTunes without burning up backups all the time.

    Hopefully some MAC tech expert with iTunes can clear this up for us. After reading past post about this subject, before I posted mine today, I really didn't get a answer. Maybe I missed something? Maybe these dups happen differently in OS operating systems, verses Windows. If I don't get a answer here, I will call Apple and ask them. One way or the other I want to clear this up, because I don't want to open my itunes a few months down the line and find duplicates again.

  • Why windowClosing is called two times?

    Hi,
    I'm saving my project in JFrame windowClosing event. But it is called two times...
    How to prevent it?
    this.addWindowListener(new java.awt.event.WindowAdapter() {
         public void windowClosing(java.awt.event.WindowEvent e) {
         saveCurrentProject();
    Thanks,
    Andr�

    Thanks, problem solved.
    this.addWindowListener(new java.awt.event.WindowAdapter() {
    public void windowClosing(java.awt.event.WindowEvent e) {
    saveCurrentProject();
    System.exit(0); // <-- new line
    Andre

  • I have change my phone language to english. but the phone shows chinese language in the internet pages and with call details. how to change it. why is this occuring?

    i have change my phone language to english. but the phone shows chinese language in the internet pages and with call details. how to change it. why is this occuring?

    If the phone is not unlocked, let's say for example it is locked to another carrier such as at&t, when you put in the Tmobile SIM in, the phone would complain about the SIM.
    Does your phone do that? If yes, then the party that sold you the iPhone4S (if you purchased it from another person) may have not done the unlocking properly, and that may explain why Tmobile is insisting on the phone being locked.
    If you purchased your iPhone4S new from Apple, did you pick Tmobile, or did you choose unlocked?
    If you picked the Tmobile choice, then it should come up ready to use.
    If you picked the unlocked choice, then you would need to visit Tmobile to purchase a nano-SIM and usually they should be more than willing to help you, since you're paying for their service.
    But as you have indicated, this phone is under AppleCare, so if it doesn't work with a supposedly good Tmobile SIM, the quickest way to fix this is to get a replacement phone. When we troubleshoot we always try the least time consuming methods first.
    You may have noticed it, that a good number of unfortunate uses of the iPhone4S have had their wifi become strangely disabled after upgrade to iOS7. Your situation is the reverse, your wifi is useable but not your cellular network.
    In the mean time, you can apply for a Google Voice account (see http://www.google.com/voice), and obtain a free North American telephone number. You can then download one of many free Google Talk applications. Set it up, and you will be able to call and receive phone calls using your wifi. This would be just for the transition period before you get a useable phone.
    Google voice provides average voice quality calls to and from US & Canada numbers for free for 1 hour per call. You can use this as a temporary work around while you are waiting for your replacement phone.
    Cheers!

  • I have just clicked on my iTunes desktop icon and got the following message "The file iTunes Library.itl" cannot be read because it was created by a newer version of iTunes. Why has this occurred when it working fine?

    I have just clicked on my iTunes desktop icon and got the following message "The file iTunes Library.itl" cannot be read because it was created by a newer version of iTunes. Why has this occurred when it working fine? Is it best to update iTunes to 10.2 to attempt to resolve? Initially I got the prompt Quicktime was out of date so I updated that successfully in all programs. I do not want to lose my iTunes library.

    Sorted problem updated and reinsatlled and restarted computer.

  • % if () {%   %} if() {% %}% Why  does Error occur?

    Hi,
    can somebody help me to understand why does error occur in this example?
    <% if (k.equals("1")) {%>
    html tags
    <%}
    if (k.equals("2")) {%>
    html tags
    <%}%>
    If there is only one condition it works, but if tere are two, the second condition lead to the error on server.
    Thanks and Best Regards,
    sergey

    that's some ugly, hard to read scriptlet code. Go learn JSTL right away.
    Impossible to tell from what you've posted. Maybe the variable k isn't in scope for both or something like that.
    Your HTML tags might be wrong, too. Who knows? You've posted so little...

  • Why static is used before any function in java

    Sir/Madem
    I'm new in java. I have just started learning java.
    please tell me
    "why static is used before any function in java??????"
    I have searched on net and in soime bokks .But i was unable toget it. what is exact reson of using static in java.

    tandm-malviya wrote:
    Sir/Madem
    I'm new in java. I have just started learning java.
    please tell me
    "why static is used before any function in java??????"
    I have searched on net and in soime bokks .But i was unable toget it. what is exact reson of using static in java.It's actually seldom used in "real" applications. static associates the method with the class instead of an instance.
    Kaj

Maybe you are looking for

  • Can I use CC on more than one device?

    Hey guys, Looking to find out if I can use CC on both my Macbook Pro and iMac or do I need to purchase an additional membership?

  • Officejet Pro 8000 A809 - can I print just black and have empty color cartridge?

    I saw in a previous post entitled "PSC 1410 - can I print just black and have empty color cartridge" that the printer does color underprinting even when printing black and white, to prevent bleeding. My question is, is this also true when selecting t

  • IE10 Win7 64-Bit Crashes continually

    I installed IE10 64-bit using automatic updates today. After rebooting and launching IE10, a popup is displayed and states, A problem caused the program to stop working correctly. Here is the IE10_main.log: 00:00.000: ================================

  • Caption being trimmed by PM 4.6.1? Or is it ACR?

    Running CS3 and ACR 4.6, on an Intel Mac running 10.5.8  and using Photo Mechanic version 4.6.1 Hi, I sometimes (unfortunately..) have to add monstrously long captions to accompany images. In an instance today I captioned NEFs in Photo Mechanic (PM)

  • Restoring a 30gig.

    HEy everyone, here's my question to all of you. I im having trouble restoring my ipod. When i turn it on i get the message "Please connect to computer. Please restore with iTunes." in 4 different languages. This is fine, i eventually got it to read a