Solved

how to close Jwindow which loose focus

Posted on 2001-08-27
30
808 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
Comment Utility
You can fire windowDeactivated.
0
 

Author Comment

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

Expert Comment

by:shekoff
Comment Utility
You can add FocusListener ! And when is fired focusLost event you can close your window !
0
 
LVL 6

Expert Comment

by:kotan
Comment Utility
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
Comment Utility
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_
Comment Utility
the only possible solution is to addFocusListener recursivelly to all Window children and analyze focus events.
0
 

Author Comment

by:sadounj
Comment Utility
well,what is the analysis??
0
 
LVL 6

Expert Comment

by:kotan
Comment Utility
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
Comment Utility
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_
Comment Utility
> 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
Comment Utility
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_
Comment Utility
> 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
Comment Utility
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
Comment Utility
heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
0
 

Expert Comment

by:lawpan
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 92

Expert Comment

by:objects
Comment Utility
> 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
Comment Utility
heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
0
 

Author Comment

by:sadounj
Comment Utility
heyhey, thanks.
your suggestion was my first thought.

can you give me some details about the algorithm.
0
 

Author Comment

by:sadounj
Comment Utility
well,what is the analysis??
0
 
LVL 16

Expert Comment

by:heyhey_
Comment Utility
you'll probably have to start another Thread.

did you try the windowDeactivated code ?
0
 

Author Comment

by:sadounj
Comment Utility
well,what is the analysis??
0
 

Author Comment

by:sadounj
Comment Utility
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_
Comment Utility
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
Comment Utility
why windowDeactivated doesn't work???
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
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_
Comment Utility
> so I can't reach them

you can reach them
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 200 total points
Comment Utility

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
Comment Utility
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
Comment Utility
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now