I tried to implement a small (test) programm using the SystemTray
in java. I got problems and wrote a (M)WE and pubushed it with http://pastebin.com/nYBkaLSy (sorry for not doing any safty checks).
What happens (under Ubuntu/KDE with openJDK):
- The program starts and the button is clickable. The
SystemTray
is registered (all right so far) - If I click (single left mouse) on the image in the tray the button is no more clickable. Instead it seems to be disabled. A right-click on the tray icon OR the frame content (!!!) opens the popup. After closing this (clicking anywhere) the button active again. (this bahavior I cannot explain. My code does not implie this as far as I see)
- If I click on the minimize button of the window manager the frame disappears (correct)
- On choosing
show
from the popup of the tray icon restores the frame and the frame content is clickable (ok so far). - Unfortunately the popup is disabled after that. I cannot open it again and if I minimize the window again I cannot bring the window up again. (also I do not see the point in the code)
Now I am unsure if I understood the principles wrongly or if there is some other bug around. SO please help me to clear my questions.
EDIT: I inserted the code (a bit modifie) here.
Please note that the created image is just dummy code. In my implementation I load an external image (see comments) which produces the same result. So the iconToImage()
hint in the comments seems not to be a problem.
import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TestTray {
public static void main(String[] args) {
// Create tmp image
Image image = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
image.getGraphics().drawOval(2, 2, 30, 30);
// Alternative: Load PNG image
//URL imUrl = TestTray.class.getResource("clock.png");
//ImageIcon icon = new ImageIcon(imUrl);
//image = icon.getImage();
TrayIcon ti = new TrayIcon(image);
ti.setImageAutoSize(true);
try {
SystemTray.getSystemTray().add(ti);
} catch (AWTException e) {
e.printStackTrace();
}
// Create JFrame and set default things
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JButton("Test-Button"));
frame.pack();
frame.setVisible(true);
// Add listener to hide window in case of minimization
frame.addWindowStateListener(new WindowStateListener() {
@Override
public void windowStateChanged(WindowEvent ev) {
if(ev.getNewState() == JFrame.ICONIFIED)
frame.setVisible(false);
}
});
// Create popup for System tray and register it
PopupMenu popup = new PopupMenu();
MenuItem menuItem;
menuItem = new MenuItem("Show");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(true);
frame.setExtendedState(JFrame.NORMAL);
}
});
popup.add(menuItem);
menuItem = new MenuItem("Exit");
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
popup.add(menuItem);
ti.setPopupMenu(popup);
}
}