• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 910
  • Last Modified:

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??!)
0
sadounj
Asked:
sadounj
  • 13
  • 7
  • 3
  • +5
1 Solution
 
kotanCommented:
You can fire windowDeactivated.
0
 
sadounjAuthor Commented:
When????  how do i know the time to fire this?
0
 
shekoffCommented:
You can add FocusListener ! And when is fired focusLost event you can close your window !
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
kotanCommented:
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);
     }
}
0
 
sadounjAuthor Commented:
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 ???
0
 
heyhey_Commented:
the only possible solution is to addFocusListener recursivelly to all Window children and analyze focus events.
0
 
sadounjAuthor Commented:
well,what is the analysis??
0
 
kotanCommented:
Try this,

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

This should work.
0
 
objectsCommented:
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();
  }
}

0
 
heyhey_Commented:
> What's wring with windowDeactivated() ?

I personally don't know if there is anything wrong :)
can't try it right now
0
 
sadounjAuthor Commented:
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???????
0
 
heyhey_Commented:
> 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)
0
 
sadounjAuthor Commented:
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???????
0
 
sadounjAuthor Commented:
heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
0
 
lawpanCommented:
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();
    }
}
0
 
objectsCommented:
> 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.

0
 
sadounjAuthor Commented:
heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
0
 
sadounjAuthor Commented:
heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
0
 
sadounjAuthor Commented:
well,what is the analysis??
0
 
heyhey_Commented:
you'll probably have to start another Thread.

did you try the windowDeactivated code ?
0
 
sadounjAuthor Commented:
well,what is the analysis??
0
 
sadounjAuthor Commented:
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 )-:
0
 
heyhey_Commented:
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.
0
 
sadounjAuthor Commented:
why windowDeactivated doesn't work???
0
 
objectsCommented:
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 :)
0
 
sadounjAuthor Commented:
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?????
0
 
heyhey_Commented:
> so I can't reach them

you can reach them
0
 
heyhey_Commented:

class xx implements FocusListener, ComponentListener
{
  // blah
  // blah
 
  void initUI()
  {
    attachToComponent(mainComponent);
  }

  public void focusGained(FocusEvent e)
  {
    isFocused = true;
    Component c = (Component)e.getSource();
    logg.log("focusGained: " + this + " / " + c);
    textFieldSubpanel.textField.requestFocus();
  }

  public void focusLost(FocusEvent e)
  {
    isFocused = false;
    Component c = (Component)e.getSource();
    logg.log("focusLost: " + this + " / " + c);
  }
 
  public void componentAdded(ContainerEvent e)
  {
    Component c = e. getChild();
    attachToComponent(c);
  }

  private void attachToComponent(Component c)  
  {
    c.addFocusListener(this);
   
    if (c instanceof Container)
    {
      Container cc = (Container)c;
      cc.addContainerListener(this);

      Component[] comps = cc.getComponents();
      for (int i = 0; i < comps.length; i++)
        attachToComponent(comps[i]);
    }
  }

  private void dettachFromComponent(Component c)  
  {
    c.removeFocusListener(this);

    if (c instanceof Container)
    {
      Container cc = (Container)c;
      cc.removeContainerListener(this);

      Component[] comps = cc.getComponents();
      for (int i = 0; i < comps.length; i++)
        dettachFromComponent(comps[i]);
    }
  }
   
  public void componentRemoved(ContainerEvent e)  
  {
    Component c = e. getChild();
    dettachFromComponent(c);
  }
 
 
}
0
 
vemulCommented:
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
0
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 13
  • 7
  • 3
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now