How to make my app icon bounce in the Mac dock

后端 未结 1 2035
感情败类
感情败类 2020-12-22 02:27

Well I\'m writing an IRC client in Java and I was wondering if there was a way to make my app\'s icon bounce in the dock when a nickalert is triggered (or any other relevant

相关标签:
1条回答
  • 2020-12-22 03:00

    Under the MacOS, try using something like Application#requestUserAttention(boolean)

    import com.apple.eawt.Application;
    ...
    Application application = Application.getApplication();
    application.requestUserAttention(false);
    

    nb- I've not tried this my self - sorry.

    Updated with example

    From the JavaDocs

    Requests user attention to this application (usually through bouncing the Dock icon). Critical requests will continue to bounce the Dock icon until the app is activated. An already active application requesting attention does nothing.

    That means, that if the application has focus, then the method will do nothing.

    Test on Mac OSX 10.7.5, Java 1.7.0_07

    import com.apple.eawt.Application;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class TestMacIcon {
    
        public static void main(String[] args) {
            new TestMacIcon();
        }
    
        public TestMacIcon() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
    
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                final Application application = Application.getApplication();
                addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        try {
                            System.out.println("clicked");
                            application.requestUserAttention(true);
                            application.setDockIconImage(ImageIO.read(getClass().getResource("/Java.png")));
                            application.setDockIconBadge("Blah");
                            application.requestUserAttention(true);
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                    }
                });
                Timer time = new Timer(2000, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (!SwingUtilities.getWindowAncestor(TestPane.this).hasFocus()) {
                            ((Timer)e.getSource()).stop();
                            System.out.println("Pay attention!!");
                            application.requestUserAttention(true);
                        }
                    }
                });
                time.setRepeats(true);
                time.setCoalesce(true);
                time.start();
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
        }
    
    }
    

    Ps make sure that you do-focus application ;)

    0 讨论(0)
提交回复
热议问题