For some reason there are processes in my app that continue even after closing the application's window and exiting. Is there something I haven't accounted for? My app periodically generates a system tray notification, but these continue even after exiting. The only way to stop it is to find the java.exe process in the Task Manager. Is there something I'm overlooking?
public class TrayNotification {
public static void execute(String firstName, String incidentNumber, String requestId, TableView tabTable) throws AWTException, MalformedURLException {
if (SystemTray.isSupported()) {
TrayNotification trayNotification = new TrayNotification();
trayNotification.displayTray(firstName, incidentNumber, requestId, tabTable);
}
}
private void displayTray(String firstName, String incidentNumber, String requestId, TableView tabTable) throws AWTException, MalformedURLException {
// Obtain a single instance of SystemTray
SystemTray tray = SystemTray.getSystemTray();
// Set TrayIcon values
Image image = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/images/tray-icon.png"));
final TrayIcon trayIcon = new TrayIcon(image, "Remedy React Notification");
trayIcon.setImageAutoSize(true);
trayIcon.setToolTip(incidentNumber);
// Add TrayIcon to SystemTray instance if possible
try {
tray.add(trayIcon);
} catch (AWTException e) {
// System.out.println("Notification could not be added.");
}
trayIcon.addActionListener(e -> {
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("/fxml/scene.fxml"));
root.requestLayout();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Test");
});
// Display TrayIcon
trayIcon.displayMessage("Hey, " + firstName, "You are now tracking " + incidentNumber + " !", TrayIcon.MessageType.INFO);
}
}
I am using System.exit(0) when the user clicks "Exit" button.
Related
I need to create a dialog (message) box with the following requirements:
It should be able to change it text during it visibility period by adding periodically 1 dot to the current text it displays.
It should be modeless to avoid blocking the work of the main process. It is only a notification message for the user that a work is being processed.
It should be displayed when the process starts and disappeared when it ends. I don't mind to call it explicitly becuase I don't know how much time the task will take in advance.
The dialog should be displayed several times, each time with a different base text.
I have tried to create a modeless dialog like this:
try{
String laf = UIManager.getSystemLookAndFeelClassName();
UIManager.setLookAndFeel(laf); // tell the UIManager to change the look and feel
}
catch (Exception e)
{
System.out.println("Unable to change look and feel");
}
m_optionPane = new JOptionPane(a_sText, JOptionPane.INFORMATION_MESSAGE, JOptionPane.DEFAULT_OPTION, null, new Object[]{}, null);
m_dialog = new JDialog();
m_dialog.setSize(200, 50);
m_dialog.setTitle(a_sTitle);
m_dialog.setModal(false);
m_dialog.setContentPane(m_optionPane);
m_dialog.setLocationRelativeTo(null);
m_dialog.setAlwaysOnTop(true);
m_dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
m_dialog.pack();
public void showDialog(String a_sText, boolean a_bDynamic){
if(a_bDynamic){
m_runnable = new DynamicWait(m_optionPane, a_sText);
m_thread = new Thread(m_runnable);
m_thread.start();
}else{
m_optionPane.setMessage(a_sText);
}
m_dialog.setVisible(true);
}
public void hideDialog2(){
if (m_thread != null){
m_thread.interrupt();
m_runnable = null;
m_thread = null;
}
m_dialog.dispose();
}
and then use a thread to constantly add the dots..
public class DynamicWait implements Runnable {
public DynamicWait(JOptionPane a_optionPane, String a_sText){
Log4jWrapper.writeLog(LogLevelEnum.WARN, "DynamicWait - " + a_sText + " Thread ID " + Thread.currentThread().getId());
m_optionPane = a_optionPane;
m_sText = a_sText;
}
#Override
public void run() {
while(m_bRun){
try {
m_optionPane.setMessage(m_sText);
Thread.sleep(1000);
m_optionPane.setMessage(m_sText + ".");
Thread.sleep(1000);
m_optionPane.setMessage(m_sText + "..");
Thread.sleep(1000);
m_optionPane.setMessage(m_sText + "...");
Thread.sleep(1000);
} catch (InterruptedException e) {
Log4jWrapper.writeLog(LogLevelEnum.WARN, e.getMessage());
}
}
}
public void stopRun(){
m_bRun = false;
}
Well, all in all, I couldn't get the proper results.
I'll appreciate your advise.
Thank you.
i am working on a java project and i want to display a message in popup like the popup of "Safe To Remove Hardware" occurred in the windows when we click on the Eject icon for USB Drives.
I want show my message in the same kind of popup using java code.
Use the SystemTray class.
To create an icon with a tooltip, use something like this:
SystemTray tray = SystemTray.getSystemTray();
TrayIcon icon = new TrayIcon(....);
icon.setToolTip("I have finished my work");
icon.setActionListener(this);
tray.add(trayIcon);
Then in the class that displays the tooltip, implement the ActionListener interface to be informed when the user clicks on the icon and/or the tooltip (that's what the setActionListener() is for)
For more details refer to the Javadocs of SystemTray, TrayIcon and ActionListener
You simply need to use the displayMessage(...) method of the TrayIcon class.
Try your hands on this code, is this what you wanted :
import java.awt.*;
import java.net.URL;
import javax.swing.*;
public class BalloonExample
{
private void createAndDisplayGUI()
{
TrayIcon trayIcon = new TrayIcon(createImage(
"/image/caIcon.png", "tray icon"));
SystemTray tray = SystemTray.getSystemTray();
try
{
tray.add(trayIcon);
}
catch (AWTException e)
{
System.out.println("TrayIcon could not be added.");
return;
}
trayIcon.displayMessage("Balloon", "My First Balloon", TrayIcon.MessageType.INFO);
}
//Obtain the image URL
protected static Image createImage(String path, String description) {
URL imageURL = BalloonExample.class.getResource(path);
if (imageURL == null) {
System.err.println("Resource not found: " + path);
return null;
} else {
return (new ImageIcon(imageURL, description)).getImage();
}
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new BalloonExample().createAndDisplayGUI();
}
});
}
}
Have a look at my question here. Basically that tooltip is a Balloon tip and you can use ShellNotifyIcon to create one.
I want to add my application to the system tray when it's window is closed (similar to the Google Talk application). And then when I click the on icon in the system tray the application window becomes active again. How can I do this in Java?
final SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("images.jpg");
final TrayIcon trayIcon = new TrayIcon(image);
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
this.addWindowStateListener(new WindowStateListener() {
public void windowStateChanged(WindowEvent e) {
if (e.getNewState() == EXIT_ON_CLOSE) {
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
setVisible(true);
}
});
setVisible(false);
}
}
});
you have set DefaultCloseOperations correctly
myFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE)
this code line is same as myFrame.setVisible(false), then for restore of JFrame from JPopupMenu to call only myFrame.setVisible(true)
I got answer. Now when i close window its closing and when i click on System tray icon then it again open my window
Image image = Toolkit.getDefaultToolkit().getImage("src/resources/ChatIcon1.jpeg");
final TrayIcon trayIcon = new TrayIcon(image);
trayIcon.setToolTip("OfficeCommunicator");
try {
SystemTray.getSystemTray().add(trayIcon);
} catch (AWTException e2) {
e2.printStackTrace();
}
trayIcon.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
trayIcon.displayMessage("hi", "You Opened Me Again", TrayIcon.MessageType.INFO);
setVisible(true);
}
});
}
How do I do an application that runs only as a SystemTray TrayIcon on mac os x, (without an awt window and dock icon)?
The code I'm using is this:
public class App
{
public static void main( String[] args )
{
final TrayIcon trayIcon;
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
trayIcon = new TrayIcon(image, "Tray Demo");
trayIcon.setImageAutoSize(true);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
} else {
System.out.println("Tray is not supported");
// System Tray is not supported
}
}
}
The problem is I'm getting a dock icon with title com.cc.ew.App
To prevent icon in dock, you must in <your-app>-Info.plist file add boolean key LSUIElement and set it to YES.
<key>LSUIElement</key>
<true/>
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?
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("your_image/path_here.gif");
// 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.
It's very simple
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class SystemTrayDemo{
//start of main method
public static void main(String []args){
//checking for support
if(!SystemTray.isSupported()){
System.out.println("System tray is not supported !!! ");
return ;
}
//get the systemTray of the system
SystemTray systemTray = SystemTray.getSystemTray();
//get default toolkit
//Toolkit toolkit = Toolkit.getDefaultToolkit();
//get image
//Toolkit.getDefaultToolkit().getImage("src/resources/busylogo.jpg");
Image image = Toolkit.getDefaultToolkit().getImage("src/images/1.gif");
//popupmenu
PopupMenu trayPopupMenu = new PopupMenu();
//1t menuitem for popupmenu
MenuItem action = new MenuItem("Action");
action.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Action Clicked");
}
});
trayPopupMenu.add(action);
//2nd menuitem of popupmenu
MenuItem close = new MenuItem("Close");
close.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
trayPopupMenu.add(close);
//setting tray icon
TrayIcon trayIcon = new TrayIcon(image, "SystemTray Demo", trayPopupMenu);
//adjust to default size as per system recommendation
trayIcon.setImageAutoSize(true);
try{
systemTray.add(trayIcon);
}catch(AWTException awtException){
awtException.printStackTrace();
}
System.out.println("end of main");
}//end of main
}//end of class
Set appropriate path for image and then run the program. t.y. :)
This is the code you can use to access and customize the system tray:
final TrayIcon trayIcon;
if (SystemTray.isSupported()) {
SystemTray tray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("tray.gif");
MouseListener mouseListener = new MouseListener() {
public void mouseClicked(MouseEvent e) {
System.out.println("Tray Icon - Mouse clicked!");
}
public void mouseEntered(MouseEvent e) {
System.out.println("Tray Icon - Mouse entered!");
}
public void mouseExited(MouseEvent e) {
System.out.println("Tray Icon - Mouse exited!");
}
public void mousePressed(MouseEvent e) {
System.out.println("Tray Icon - Mouse pressed!");
}
public void mouseReleased(MouseEvent e) {
System.out.println("Tray Icon - Mouse released!");
}
};
ActionListener exitListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Exiting...");
System.exit(0);
}
};
PopupMenu popup = new PopupMenu();
MenuItem defaultItem = new MenuItem("Exit");
defaultItem.addActionListener(exitListener);
popup.add(defaultItem);
trayIcon = new TrayIcon(image, "Tray Demo", popup);
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Action Event",
"An Action Event Has Been Performed!",
TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
try {
tray.add(trayIcon);
} catch (AWTException e) {
System.err.println("TrayIcon could not be added.");
}
} else {
// System Tray is not supported
}