JFrame bring to front and getting the focus



I know there have been a lot of discussions about this topic, but i
couldn't find the right answer.
Tested on JDK 1.4 and 1.5 under windows XP sp2

The scenario is very simple, I have telephone application written in
Swing, when the application lost the focus (the user clicked another
windows application) the telephone lost the focus. All I want to do is
when I receive a call to pop up the JFrame (telephone) and to be the
top most window being displayed.

This is what I'm doing when a call is received
myFrame.setVisible(true);
myFrame.setExtendedState(JFrame.NORMAL);
myFrame.toFront();
myFrame.requestFocus();

and many combinations. :-(

I found the following problem.
Scenario:
JFrame in front.
Click another win app.
Received a call
JFrame pop up
The user doesn't click on the JFrame, but instead click on another app.
(meaning that I don't see my JFrame)
I receive again a call to pop up my JFrame.
This time the JFrame doesn't pop up.

btw I've tried also myFrame.setAllwayOnTop(true) and then setting it to
false and the described problem is not happening, but new problems
arrise :-(

Here is the source with an example of what is happening.

Any idea how to solve this without using native calls?


import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.awt.event.WindowListener;
import java.awt.event.WindowStateListener;
import java.awt.peer.ComponentPeer;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;

public class ToFrontTest extends JFrame implements WindowStateListener,
WindowFocusListener, WindowListener{
int delay = 5000; // delay for 5 sec. before starting
int period = 10000; // repeat every 10 sec.
Timer timer;
JButton btnStart;
JButton btnStop;

public ToFrontTest(){
setPreferredSize(new Dimension(200,200));
initComponents();
addListeners();
setFocusable(true);
}

private void addListeners() {
addWindowStateListener(this);
addWindowFocusListener(this);
addWindowListener(this);
btnStart.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
btnStart.setEnabled(false);
btnStop.setEnabled(true);
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
System.out.println("## setting visible");
if(!isVisible()){
setVisible(true);
}
setExtendedState(JFrame.NORMAL);
//setAlwaysOnTop(true);
//setAlwaysOnTop(false);
System.out.println("## sending to front");
toFront();
requestFocus();
}
}, delay, period);
}

});

btnStop.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {
System.out.println("## stoping timer");
timer.cancel();
btnStart.setEnabled(true);
btnStop.setEnabled(false);
}

});
}

private void initComponents() {
btnStart = new JButton("Start timer");
btnStop = new JButton("Stop timer");
setLayout(new FlowLayout());
add(btnStart);
add(btnStop);
btnStart.setEnabled(true);
btnStop.setEnabled(false);
}

/**
* @param args
*/
public static void main(String[] args) {
JFrame test = new ToFrontTest();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test.pack();
test.setVisible(true);
}

public void windowStateChanged(WindowEvent e) {
System.out.println("****************** Window state changed: " +
e.getNewState());

}

public void windowGainedFocus(WindowEvent e) {
System.out.println("Focus gained: " + e.getNewState());

}

public void windowLostFocus(WindowEvent e) {
System.out.println("Focus lost: " + e.getNewState());

}

public void windowOpened(WindowEvent e) {
System.out.println("Opened");

}

public void windowClosing(WindowEvent e) {
System.out.println("closing");

}

public void windowClosed(WindowEvent e) {
System.out.println("closed");
}

public void windowIconified(WindowEvent e) {
System.out.println("iconified");
}

public void windowDeiconified(WindowEvent e) {
System.out.println("deiconified");
}

public void windowActivated(WindowEvent e) {
System.out.println("activated");
}

public void windowDeactivated(WindowEvent e) {
System.out.println("deactivated");
}

}

.



Relevant Pages

  • Re: PaintComponent Does not Fire
    ... public class ViewGraphical extends JFrame implements Observer ... private Controller controller; ... Image imgDog = Toolkit.getDefaultToolkit.getImage; ... public void update ...
    (comp.lang.java.gui)
  • Re: Canvas
    ... I added a Canvas in a JFrame. ... public void actionPerformed{ ... public class MyCanvas2 extends Canvas{ ...
    (comp.lang.java.programmer)
  • Re: Drawing Image
    ... put on to a JFrame which has several internal frames. ... public class test extends JPanel { ... public void paintComponent{ ...
    (comp.lang.java.help)
  • Re: Drawing translucent masks
    ... I ommited the lines that create the JFrame instance. ... private static Image[] TileGfx; ... public void actionPerformed{ ... int h = getHeight; ...
    (comp.lang.java.help)
  • Help Menu - opens frame which does not go to front
    ... I'm using a JFrame to display help content in a JEditorPane. ... the help frame does not have focus nor is it at the ... public void menuSelected ...
    (comp.lang.java.gui)