Inconsistent behavior of JFrame.toFront()
Hi,
I have a simple chat client application using Swing running on J2SE 5 (1.5.0_04) on Windows XP SP2.
When a new message arrives I would like the choice of between 2 behaviors:
1) the window (a JFrame) to come to the front.
2) tray icon flashes and windows remains unchanged
Problem is when I try to do 1) by calling toFront() it works the first time I call it, the subsequent calls to toFront() result in the tray icon to flash 3 times and the highlighted.
So my calls to toFront result in a mixture of behavior 1) and 2)!
My question is: how can I get consistent behaviors 1) or 2)?
Is there a clear description of the toFront behavior (the javadoc
is a little vague)?
Any help with this matter would be greatly appreciated.
Thanks,
Florian
[823 byte] By [
FlorianKa] at [2007-11-27 2:39:51]

# 1
Well, I personally don't like option 1 since when the frame comes to the front it also steals focus, which is pain when you are busy typing data into some other application. So I would prefer option 2. However, I have no idea how to induce option 2.
Using JDK1.4.2 on XP the following code will always make the frame come to the front:
import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
public class FrameToFront
{
public static void main(String[] args)
{
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(200, 200);
frame.setLocationRelativeTo( null );
frame.setVisible(true);
final Timer timer = new Timer(5000, new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
frame.toFront();
}
});
timer.start();
}
}
# 2
I agree with you option 1 is not my favorite either, it's an option that some users requested.
I tried your snippet of code and ran it on my box. It's inconsistent again... except this time it depends the window in front of it!!!
If I start the program from a command line prompt, yes it comes in front everytime. If I place this IE window in front, no it flashes in the task bar.
If I put an Outlook window in front then yes it pops up in front.
If I read this in someone else's thread I would have my doubts.
This is bewildering, to say the least...