0

Im trying to figure out how i can add number notifications to my dock icon when a new message is received (I've got a small chat app)

This is what i mean:

enter image description here

Any ideas how to accomplish this?

martinez314
  • 12,162
  • 5
  • 36
  • 63
Alosyius
  • 8,771
  • 26
  • 76
  • 120
  • 1
    What you're looking for is called a "badge." This doesn't answer your specific question but may help, if you don't mind generating your own badged icon images at runtime (not great, I know): http://stackoverflow.com/q/6006173/139010. This might suffice entirely: http://eclipse.dzone.com/articles/badge-label-mac-dock-icon – Matt Ball Nov 19 '13 at 17:28

1 Answers1

4

Try taking a look at com.apple.eawt.Application (I'm having a hard time finding JavaDocs)

Simple Counter

import com.apple.eawt.Application;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestBadge {

    public static void main(String[] args) {
        new TestBadge();
    }

    public TestBadge() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private JLabel label;
        private int count;

        public TestPane() {
            setLayout(new GridBagLayout());
            add((label = new JLabel("0")));
            Timer timer = new Timer(250, new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    count++;
                    label.setText(Integer.toString(count));
                    Application.getApplication().setDockIconBadge(Integer.toString(count));
                }
            });
            timer.start();
        }

    }

}

I should point out, that if you're running in an IDE capable of inspecting the installed classes, you should be able to get a list of the functionality provided by the Application class. I was using NetBeans and was able to find a listing of all the methods, problem is, some were documented and some weren't :P

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366