Skip to main content

Flashing JFrame

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
ilyvatar
Offline
Joined: 2009-10-16

Hi,

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>
<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>
<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>
<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>
<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>
<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:
http://www.java-forums.org/awt-swing/35443-flashing-jframe.html#post160074
http://stackoverflow.com/questions/4305837/flashing-jframe
http://forums.oracle.com/forums/thread.jspa?threadID=2137438&stqc=true
http://www.coderanch.com/t/518839/GUI/java/Flashing-JFrame#2349181

Thanks!

Reply viewing options

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

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