Flashing jframe

I would like to create a JFrame with two specific functions:

  • JFrame does not have to capture focus, but is maximized from the minimized state.
  • When a JFrame has created or been maximized from a minimized state, it should blink in the Windows panel until the user provides it with focus. (for example, in ICQ clients alt text )

Does anyone know how to implement the second requirement?

A bit of a self-evident example:

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

Thanks!

+4
source share
2 answers

The following code worked for me exactly as you described:

  f.setState(JFrame.ICONIFIED); f.toFront(); 

f is a JFrame.

+1
source

Unfortunately, this is not something you can do natively under any Java platform. Anyone who manages to get him to work using such a โ€œtrickโ€ that you showed will be disappointed that he is unlikely to work on another version of Windows or even on another computer with the same version of Windows. The only time I have ever seen a Java window flash is caused by some Swing crashing while minimizing all windows on the taskbar.

How this article about how Java applications feel native is the same thing on Mac OS.

Itโ€™s best to use the methods described in this article to create a JNI that calls the Windows API call, or obtain a license for JNIWrapper (find it) that does all this for you (the best option is if you are creating a commercial application or making it for a client who is willing to pay for such a function). It looks like you can get a 30-day trial for this.

The only thing I could offer is to create the equivalent of a bad person for a pop-up notification system. When you want to warn the user, create a frame without a frame, place it in the lower right corner of the screen, make it inactive, and show it for a short period of time.

+1
source

All Articles