Solved

how to close Jwindow which loose focus

Posted on 2001-08-27
30
824 Views
Last Modified: 2013-11-23
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
Comment
Question by:sadounj
  • 13
  • 7
  • 3
  • +5
30 Comments
 
LVL 6

Expert Comment

by:kotan
ID: 6430949
You can fire windowDeactivated.
0
 

Author Comment

by:sadounj
ID: 6430952
When????  how do i know the time to fire this?
0
 
LVL 1

Expert Comment

by:shekoff
ID: 6430973
You can add FocusListener ! And when is fired focusLost event you can close your window !
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 6

Expert Comment

by:kotan
ID: 6430999
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
 

Author Comment

by:sadounj
ID: 6431003
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 6431030
the only possible solution is to addFocusListener recursivelly to all Window children and analyze focus events.
0
 

Author Comment

by:sadounj
ID: 6431041
well,what is the analysis??
0
 
LVL 6

Expert Comment

by:kotan
ID: 6431122
Try this,

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

This should work.
0
 
LVL 92

Expert Comment

by:objects
ID: 6431125
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 6431149
> What's wring with windowDeactivated() ?

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

Author Comment

by:sadounj
ID: 6431173
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 6431182
> 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
 

Author Comment

by:sadounj
ID: 6431193
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
 

Author Comment

by:sadounj
ID: 6431199
heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
0
 

Expert Comment

by:lawpan
ID: 6431217
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
 
LVL 92

Expert Comment

by:objects
ID: 6431249
> 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
 

Author Comment

by:sadounj
ID: 6431298
heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
0
 

Author Comment

by:sadounj
ID: 6431330
heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
0
 

Author Comment

by:sadounj
ID: 6431470
well,what is the analysis??
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 6431504
you'll probably have to start another Thread.

did you try the windowDeactivated code ?
0
 

Author Comment

by:sadounj
ID: 6431527
well,what is the analysis??
0
 

Author Comment

by:sadounj
ID: 6431535
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 6431580
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
 

Author Comment

by:sadounj
ID: 6431591
why windowDeactivated doesn't work???
0
 
LVL 92

Expert Comment

by:objects
ID: 6431717
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
 

Author Comment

by:sadounj
ID: 6431746
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 6431925
> so I can't reach them

you can reach them
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 200 total points
ID: 6431934

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
 
LVL 5

Expert Comment

by:vemul
ID: 7755839
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
 

Expert Comment

by:SpideyMod
ID: 7847033
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question