Link to home
Start Free TrialLog in
Avatar of sadounj
sadounj

asked on

how to close Jwindow which loose focus

Hellow,
I Have JWindow which contains number of text fields and buttons.

The window will open in case the user clicks on a <Open window > button. And I want to close the window in case thw user click outside the window (or lost focus??!)
Avatar of kotan
kotan
Flag of Malaysia image

You can fire windowDeactivated.
Avatar of sadounj
sadounj

ASKER

When????  how do i know the time to fire this?
You can add FocusListener ! And when is fired focusLost event you can close your window !
Sorry! windowDeactivated is not worked.
Like shekoff said you should add FocusListener

add this to your code.

addFocusListener(new java.awt.event.FocusAdapter()) {
     public void focusLost(java.awt.event.FocusEvent ev) {
          System.exit(1);
     }
}
Avatar of sadounj

ASKER

all this thigs I already done but here is an example when this is not working:

if you press on one of the buutons inside the window, and now you press outside the window the focus is not lost from the window!!

I thought that i had to register ALL the components inside the window with FocusListener. but what to do in focusLost?? how whould I know if the focus was lost because the user press another button in the window OR pressed outside the window ???
the only possible solution is to addFocusListener recursivelly to all Window children and analyze focus events.
Avatar of sadounj

ASKER

well,what is the analysis??
Try this,

addWindowListener(new java.awt.event.WindowAdapter() {
     public void windowDeactivated(WindowEvent evt) {
          System.exit(1);
     }
}

This should work.
Avatar of Mick Barry
What's wring with windowDeactivated() ?
Add a window listener to your window and in the windowDeactivated() method close the window.

public class WindowCLoser extends WindowAdapter
{
  public void windowDeactivated(WindowEvent e)
  {
    e.getWindow().hide();
  }
}

> What's wring with windowDeactivated() ?

I personally don't know if there is anything wrong :)
can't try it right now
Avatar of sadounj

ASKER

well i assume that you didn't understood the situation...

I WANT TO KNOW WHEN! TO CLOSE , I Know how to close but dont know When???????
> I WANT TO KNOW WHEN! TO CLOSE

my suggestion is to track the focus movement and close the window when all child coponents do not have focus for a 1-2 seconds.

objects & kotan is to close the window when it is deactivated (you must add WindowListener)
Avatar of sadounj

ASKER

well i assume that you didn't understood the situation...

I WANT TO KNOW WHEN! TO CLOSE , I Know how to close but dont know When???????
Avatar of sadounj

ASKER

heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
public class MAIN {

    /** Creates new MAIN */
    public MAIN() {
    }

    static JWindow window = new JWindow();
    /**
    * @param args the command line arguments
    */
    public static void main (String args[]) {
        JFrame f = new JFrame();
        f.setSize(new Dimension(450,560));
        f.setLocation(100,100);
        f.show();
        WindowListener l = new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
            public void windowClosed(WindowEvent e) {
            }
        };
        f.addWindowListener(l);
        window.getContentPane().setLayout(new FlowLayout());
        window.getContentPane().add(new JButton("hehe"));
        window.getContentPane().add(new JButton("hehe1"));
        window.getContentPane().add(new JButton("hehe2"));
        window.setSize(new Dimension(100,100));
        window.setLocation(400,400);
        window.addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowDeactivated(WindowEvent evt)
        {
             System.out.println("kalle");
             
        }
        });

        FocusListener focusListener = new FocusListener()
        {
            public void focusLost(FocusEvent e)
            {
                System.out.println(e.getSource());
                dis();
            }
            public void focusGained(FocusEvent e) { }
        };
       
        for (int i = 0;i<window.getContentPane().getComponentCount();i++)
            window.getContentPane().getComponent(i).addFocusListener(focusListener);
        window.addFocusListener(focusListener);
        window.show();
    }
    static private void dis()
    {
        window.dispose();
    }
}
> I WANT TO KNOW WHEN! TO CLOSE

WHEN! THE WINDOW IS DEACTIVATED :-)

To do this add the window listener is specified above when u create your window.

Avatar of sadounj

ASKER

heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
Avatar of sadounj

ASKER

heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
Avatar of sadounj

ASKER

well,what is the analysis??
you'll probably have to start another Thread.

did you try the windowDeactivated code ?
Avatar of sadounj

ASKER

well,what is the analysis??
Avatar of sadounj

ASKER

I tried your  code (focus...)
it work in your example but not in mine
in my window the window contains a calendar bean so I dont have access to the buttons in it?

about the ....
public class WindowCLoser extends WindowAdapter
{
 public void windowDeactivated(WindowEvent e)
 {
   e.getWindow().hide();
 }
}

it does not work on JWindow , In jframe it works )-:
re analyzis - you can create new 1-second Timer object upon each focusLost() (on the widnow and it's subcomponents) and close the window if you haven't received focusReceived() when timer is triggered.
Avatar of sadounj

ASKER

why windowDeactivated doesn't work???
Apparent only frames and dialogs fire window deactivated events :(

http://developer.java.sun.com/developer/bugParade/bugs/4123
895.html

U learn something every day :)
Avatar of sadounj

ASKER

OK now I'm frustrated :
1) can't catch windowDeactivated()

2) can't use the Focus stuff. because i put in a jwindow a 3rd party JPanel which contains a lot of swing components;
so I can't reach them, to register them to focus listener.

)-:  )-:  )-:
what would I do?????
> so I can't reach them

you can reach them
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- points to heyhey_

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange