Skip to main content

Flashing JFrame

Please note these forums are being decommissioned and use the new and improved forums at
1 reply [Last post]
Joined: 2009-10-16


I would like to create a JFrame with two specifal features:

JFrame should not grab focus while maximized from minimized state.
When a JFrame created or became maximized from minimized state, it should flash in the Windows bar until a user will grant a focus to it. (like as in ICQ clients ).
Does anybody know how the second requirement can be implemented?

Little self-explained example:

<br />
import javax.swing.*;<br />
import java.awt.event.*;<br />
import java.awt.*;</p>
<p>public class JFrameTest {<br />
    private static JFrame childFrame;<br />
    public static Container getParentContentPane() {<br />
        JPanel panel = new JPanel();<br />
        JButton button = new JButton("Create\\Restore child frame");<br />
        button.addActionListener(new ActionListener(){<br />
            public void actionPerformed(ActionEvent e) {<br />
                createOrRestoreChildFrame();<br />
            }<br />
        });<br />
        panel.add(button);<br />
        return panel;<br />
<p>    private static void createOrRestoreChildFrame() {<br />
        if (childFrame == null) {<br />
            childFrame = new JFrame("Child Frame");<br />
            childFrame.setLocation(200, 200);<br />
            childFrame.add(new JLabel("Child Frame"));<br />
            childFrame.pack();<br />
            setChildFrameVisible();<br />
        } else {<br />
            setChildFrameVisible();<br />
        }<br />
<p>    private static void setChildFrameVisible() {<br />
        childFrame.setFocusableWindowState(false);<br />
        childFrame.setVisible(true);<br />
        flashInWindowsBar(childFrame);<br />
        childFrame.toFront();<br />
        childFrame.setFocusableWindowState(true);<br />
<p>    /**<br />
     * Should Make child frame flash in Windows bar.<br />
     * Currently, it does not work for me.<br />
     * Could anybody help me to fix this please? )<br />
     */<br />
    private static void flashInWindowsBar(JFrame childFrame) {<br />
        childFrame.setState(JFrame.ICONIFIED);<br />
        childFrame.toFront();<br />
<p>    private static void createAndShowGUI() {<br />
        JFrame parentFrame = new JFrame("JFrame Demo");<br />
        parentFrame.setLocation(100, 100);<br />
        parentFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />
        parentFrame.setContentPane(getParentContentPane());<br />
        parentFrame.pack();<br />
        parentFrame.setVisible(true);<br />
<p>    public static void main(String[] args) {<br />
        javax.swing.SwingUtilities.invokeLater(new Runnable() {<br />
            public void run() {<br />
                createAndShowGUI();<br />
            }<br />
        });<br />
    }<br />
}<br />

This question rised on:


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2009-10-16

sorry for <p> taqgs, I really don't know how to fight it (