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??!)
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??!)
You can fire windowDeactivated.
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.FocusAdapte r()) {
public void focusLost(java.awt.event.F ocusEvent ev) {
System.exit(1);
}
}
Like shekoff said you should add FocusListener
add this to your code.
addFocusListener(new java.awt.event.FocusAdapte
public void focusLost(java.awt.event.F
System.exit(1);
}
}
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 ???
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.
ASKER
well,what is the analysis??
Try this,
addWindowListener(new java.awt.event.WindowAdapt er() {
public void windowDeactivated(WindowEv ent evt) {
System.exit(1);
}
}
This should work.
addWindowListener(new java.awt.event.WindowAdapt
public void windowDeactivated(WindowEv
System.exit(1);
}
}
This should work.
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(WindowEv ent e)
{
e.getWindow().hide();
}
}
Add a window listener to your window and in the windowDeactivated() method close the window.
public class WindowCLoser extends WindowAdapter
{
public void windowDeactivated(WindowEv
{
e.getWindow().hide();
}
}
> What's wring with windowDeactivated() ?
I personally don't know if there is anything wrong :)
can't try it right now
I personally don't know if there is anything wrong :)
can't try it right now
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 , 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)
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)
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 , I Know how to close but dont know When???????
ASKER
heyhey, thanks.
your suggestion was my first thought.
can you give me some details about the algorithm.
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().se tLayout(ne w FlowLayout());
window.getContentPane().ad d(new JButton("hehe"));
window.getContentPane().ad d(new JButton("hehe1"));
window.getContentPane().ad d(new JButton("hehe2"));
window.setSize(new Dimension(100,100));
window.setLocation(400,400 );
window.addWindowListener(n ew java.awt.event.WindowAdapt er() {
public void windowDeactivated(WindowEv ent evt)
{
System.out.println("kalle" );
}
});
FocusListener focusListener = new FocusListener()
{
public void focusLost(FocusEvent e)
{
System.out.println(e.getSo urce());
dis();
}
public void focusGained(FocusEvent e) { }
};
for (int i = 0;i<window.getContentPane( ).getCompo nentCount( );i++)
window.getContentPane().ge tComponent (i).addFoc usListener (focusList ener);
window.addFocusListener(fo cusListene r);
window.show();
}
static private void dis()
{
window.dispose();
}
}
/** 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().se
window.getContentPane().ad
window.getContentPane().ad
window.getContentPane().ad
window.setSize(new Dimension(100,100));
window.setLocation(400,400
window.addWindowListener(n
public void windowDeactivated(WindowEv
{
System.out.println("kalle"
}
});
FocusListener focusListener = new FocusListener()
{
public void focusLost(FocusEvent e)
{
System.out.println(e.getSo
dis();
}
public void focusGained(FocusEvent e) { }
};
for (int i = 0;i<window.getContentPane(
window.getContentPane().ge
window.addFocusListener(fo
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.
WHEN! THE WINDOW IS DEACTIVATED :-)
To do this add the window listener is specified above when u create your window.
ASKER
heyhey, thanks.
your suggestion was my first thought.
can you give me some details about the algorithm.
your suggestion was my first thought.
can you give me some details about the algorithm.
ASKER
heyhey, thanks.
your suggestion was my first thought.
can you give me some details about the algorithm.
your suggestion was my first thought.
can you give me some details about the algorithm.
ASKER
well,what is the analysis??
you'll probably have to start another Thread.
did you try the windowDeactivated code ?
did you try the windowDeactivated code ?
ASKER
well,what is the analysis??
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(WindowEv ent e)
{
e.getWindow().hide();
}
}
it does not work on JWindow , In jframe it works )-:
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(WindowEv
{
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.
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 :)
http://developer.java.sun.com/developer/bugParade/bugs/4123
895.html
U learn something every day :)
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?????
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
you can reach them
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
SpideyMod
Community Support Moderator @Experts Exchange