Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

java - How to make my app icon bounce in the Mac dock

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 notification).

If Windows also has some sort of notification system I'd like to know about it as well.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...