views:

1818

answers:

2

I have a little control-panel, just a little application that I made. I would like to minimize/put the control-panel up/down with the systemicons, together with battery life, date, networks etc.

Anyone that can give me a clue, link to a tutorial or something to read?

+14  A: 

As of Java 6, this is supported in the SystemTray and TrayIcon classes. SystemTray has a pretty extensive example in its Javadocs:

TrayIcon trayIcon = null;
if (SystemTray.isSupported()) {
    // get the SystemTray instance
    SystemTray tray = SystemTray.getSystemTray();
    // load an image
    Image image = Toolkit.getDefaultToolkit.getImage(...);
    // create a action listener to listen for default action executed on the tray icon
    ActionListener listener = new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            // execute default action of the application
            // ...
        }
    };
    // create a popup menu
    PopupMenu popup = new PopupMenu();
    // create menu item for the default action
    MenuItem defaultItem = new MenuItem(...);
    defaultItem.addActionListener(listener);
    popup.add(defaultItem);
    /// ... add other items
    // construct a TrayIcon
    trayIcon = new TrayIcon(image, "Tray Demo", popup);
    // set the TrayIcon properties
    trayIcon.addActionListener(listener);
    // ...
    // add the tray image
    try {
        tray.add(trayIcon);
    } catch (AWTException e) {
        System.err.println(e);
    }
    // ...
} else {
    // disable tray option in your application or
    // perform other actions
    ...
}
// ...
// some time later
// the application state has changed - update the image
if (trayIcon != null) {
    trayIcon.setImage(updatedImage);
}
// ...

You could also check out this article, or this tech tip.

Michael Myers
Works on OS X, too.
Markus Lux
@Markus Lux: Good to know. (I thought Apple never got around to releasing Java 6, or am I out of date?)
Michael Myers
+1  A: 

When I first needed help, I wasn't sure how to put a name on what I wanted.. But Jim Puls edited my tags and some of my title (thanks man :)).. So I guess I got my answer now then, I'll have a look at SystemTray and TrayIcon mmyers, thanks ;)

Johannes
Yeah, it does rather make the obvious response seem like "just flipping google it/read the forgotten docs". The dangers of editing. Looking back at the original, it's worth upvotes I think.
Tom Hawtin - tackline
Well, I did answer before the edits (or rather simultaneously). :)
Michael Myers
Just so you know, this 'answer' probably should have been a comment on your original question. It would get confusing if the question author (and/or others) had comments like this as answers like this if this question got more answers.
Kevin M.
I'm new to this:P Sorry
Johannes
Another thing: If you liked my answer, could you consider accepting it?
Michael Myers
How do I do that? :D
Johannes
Click the check mark below the voting icons on my answer. (It gives us both reputation, too!)
Michael Myers
Thanks! (And check out your new badge!)
Michael Myers