Java system tray hiding and restoring application
Hey all I have this java code here that creates the icon in the system trey by following THIS example:
public static void createTray() {
//Check the SystemTray is supported
if (!SystemTray.isSupported()) {
JOptionPane.showMessageDialog(null, "SystemTray is not supported", "DOH! An error", JOptionPane.ERROR_MESSAGE);
return;
}
URL imageURL = winBuilder.class.getResource("/bulb.gif");
trayIcon = new TrayIcon((new ImageIcon(imageURL, "")).getImage());
tray = SystemTray.getSystemTray();
trayIcon.setImageAutoSize(true);
// Create a pop-up menu components
MenuItem aboutItem = new MenuItem("About");
MenuItem exitItem = new MenuItem("Exit");
//Add components to pop-up menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
JOptionPane.showMessageDialog(null, "TrayIcon could not be added: " + e.getLocalizedMessage(), "DOH! An error", JOptionPane.ERROR_MESSAGE);
}
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Double-click action on icon
frmUftWebui.setVisible(true);
frmUftWebui.repaint();
SystemTray.getSystemTray().remove(trayIcon);
}
});
aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"This dialog box is run from the About menu item");
}
});
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Exit", "The cool program has closed.", TrayIcon.MessageType.INFO); //ERROR | WARNING | INFO | NONE
tray.remove(trayIcon);
System.exit(0);
}
});
}
This does work and shows the icon in the system trey as it should but once I click on the icon twice it brings up a new window instead of my original window?
This is the MAIN code that I have:
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
try {
frmUftWebui.setVisible(false);
frmUftWebui.getContentPane().setLayout(null);
frmUftWebui.setState(frmUftWebui.ICONIFIED);
// Web HTML panel
jfxPanel = new JFXPanel();
jfxPanel.setBackground(Color.WHITE);
jfxPanel.setBounds(0, 0, 1098, 523);
frmUftWebui.getContentPane().add(jfxPanel);
javafx.application.Platform.runLater(new Runnable() {
@SuppressWarnings({ "unchecked", "unused" })
public void run() {
createTray();
............................
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error: " + e.getLocalizedMessage(), "DOH! An error", JOptionPane.ERROR_MESSAGE);
}
}
What am I missing in order to get it to open the original window instead of creating a new one once I click the icon in the system tray?
java swing javafx java-8 system-tray
add a comment |
Hey all I have this java code here that creates the icon in the system trey by following THIS example:
public static void createTray() {
//Check the SystemTray is supported
if (!SystemTray.isSupported()) {
JOptionPane.showMessageDialog(null, "SystemTray is not supported", "DOH! An error", JOptionPane.ERROR_MESSAGE);
return;
}
URL imageURL = winBuilder.class.getResource("/bulb.gif");
trayIcon = new TrayIcon((new ImageIcon(imageURL, "")).getImage());
tray = SystemTray.getSystemTray();
trayIcon.setImageAutoSize(true);
// Create a pop-up menu components
MenuItem aboutItem = new MenuItem("About");
MenuItem exitItem = new MenuItem("Exit");
//Add components to pop-up menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
JOptionPane.showMessageDialog(null, "TrayIcon could not be added: " + e.getLocalizedMessage(), "DOH! An error", JOptionPane.ERROR_MESSAGE);
}
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Double-click action on icon
frmUftWebui.setVisible(true);
frmUftWebui.repaint();
SystemTray.getSystemTray().remove(trayIcon);
}
});
aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"This dialog box is run from the About menu item");
}
});
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Exit", "The cool program has closed.", TrayIcon.MessageType.INFO); //ERROR | WARNING | INFO | NONE
tray.remove(trayIcon);
System.exit(0);
}
});
}
This does work and shows the icon in the system trey as it should but once I click on the icon twice it brings up a new window instead of my original window?
This is the MAIN code that I have:
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
try {
frmUftWebui.setVisible(false);
frmUftWebui.getContentPane().setLayout(null);
frmUftWebui.setState(frmUftWebui.ICONIFIED);
// Web HTML panel
jfxPanel = new JFXPanel();
jfxPanel.setBackground(Color.WHITE);
jfxPanel.setBounds(0, 0, 1098, 523);
frmUftWebui.getContentPane().add(jfxPanel);
javafx.application.Platform.runLater(new Runnable() {
@SuppressWarnings({ "unchecked", "unused" })
public void run() {
createTray();
............................
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error: " + e.getLocalizedMessage(), "DOH! An error", JOptionPane.ERROR_MESSAGE);
}
}
What am I missing in order to get it to open the original window instead of creating a new one once I click the icon in the system tray?
java swing javafx java-8 system-tray
The system tray feature you’re using is part of AWT, so there’s no sense in performing its creation in the JavaFX thread. It’s also inconsistent to install the Windows look&feel but then setting Metal look&feel specific properties. Besides that, useUIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
instead of hardcoding a class name that is not part of the public API. But to your specific question, a call to.setVisible(true)
will never create a new window; if you see two windows then, you already created them before that event.
– Holger
Jan 7 at 16:56
add a comment |
Hey all I have this java code here that creates the icon in the system trey by following THIS example:
public static void createTray() {
//Check the SystemTray is supported
if (!SystemTray.isSupported()) {
JOptionPane.showMessageDialog(null, "SystemTray is not supported", "DOH! An error", JOptionPane.ERROR_MESSAGE);
return;
}
URL imageURL = winBuilder.class.getResource("/bulb.gif");
trayIcon = new TrayIcon((new ImageIcon(imageURL, "")).getImage());
tray = SystemTray.getSystemTray();
trayIcon.setImageAutoSize(true);
// Create a pop-up menu components
MenuItem aboutItem = new MenuItem("About");
MenuItem exitItem = new MenuItem("Exit");
//Add components to pop-up menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
JOptionPane.showMessageDialog(null, "TrayIcon could not be added: " + e.getLocalizedMessage(), "DOH! An error", JOptionPane.ERROR_MESSAGE);
}
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Double-click action on icon
frmUftWebui.setVisible(true);
frmUftWebui.repaint();
SystemTray.getSystemTray().remove(trayIcon);
}
});
aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"This dialog box is run from the About menu item");
}
});
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Exit", "The cool program has closed.", TrayIcon.MessageType.INFO); //ERROR | WARNING | INFO | NONE
tray.remove(trayIcon);
System.exit(0);
}
});
}
This does work and shows the icon in the system trey as it should but once I click on the icon twice it brings up a new window instead of my original window?
This is the MAIN code that I have:
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
try {
frmUftWebui.setVisible(false);
frmUftWebui.getContentPane().setLayout(null);
frmUftWebui.setState(frmUftWebui.ICONIFIED);
// Web HTML panel
jfxPanel = new JFXPanel();
jfxPanel.setBackground(Color.WHITE);
jfxPanel.setBounds(0, 0, 1098, 523);
frmUftWebui.getContentPane().add(jfxPanel);
javafx.application.Platform.runLater(new Runnable() {
@SuppressWarnings({ "unchecked", "unused" })
public void run() {
createTray();
............................
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error: " + e.getLocalizedMessage(), "DOH! An error", JOptionPane.ERROR_MESSAGE);
}
}
What am I missing in order to get it to open the original window instead of creating a new one once I click the icon in the system tray?
java swing javafx java-8 system-tray
Hey all I have this java code here that creates the icon in the system trey by following THIS example:
public static void createTray() {
//Check the SystemTray is supported
if (!SystemTray.isSupported()) {
JOptionPane.showMessageDialog(null, "SystemTray is not supported", "DOH! An error", JOptionPane.ERROR_MESSAGE);
return;
}
URL imageURL = winBuilder.class.getResource("/bulb.gif");
trayIcon = new TrayIcon((new ImageIcon(imageURL, "")).getImage());
tray = SystemTray.getSystemTray();
trayIcon.setImageAutoSize(true);
// Create a pop-up menu components
MenuItem aboutItem = new MenuItem("About");
MenuItem exitItem = new MenuItem("Exit");
//Add components to pop-up menu
popup.add(aboutItem);
popup.addSeparator();
popup.add(exitItem);
trayIcon.setPopupMenu(popup);
try {
tray.add(trayIcon);
} catch (AWTException e) {
JOptionPane.showMessageDialog(null, "TrayIcon could not be added: " + e.getLocalizedMessage(), "DOH! An error", JOptionPane.ERROR_MESSAGE);
}
trayIcon.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//Double-click action on icon
frmUftWebui.setVisible(true);
frmUftWebui.repaint();
SystemTray.getSystemTray().remove(trayIcon);
}
});
aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null,
"This dialog box is run from the About menu item");
}
});
exitItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
trayIcon.displayMessage("Exit", "The cool program has closed.", TrayIcon.MessageType.INFO); //ERROR | WARNING | INFO | NONE
tray.remove(trayIcon);
System.exit(0);
}
});
}
This does work and shows the icon in the system trey as it should but once I click on the icon twice it brings up a new window instead of my original window?
This is the MAIN code that I have:
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
} catch (UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
/* Turn off metal's use of bold fonts */
UIManager.put("swing.boldMetal", Boolean.FALSE);
try {
frmUftWebui.setVisible(false);
frmUftWebui.getContentPane().setLayout(null);
frmUftWebui.setState(frmUftWebui.ICONIFIED);
// Web HTML panel
jfxPanel = new JFXPanel();
jfxPanel.setBackground(Color.WHITE);
jfxPanel.setBounds(0, 0, 1098, 523);
frmUftWebui.getContentPane().add(jfxPanel);
javafx.application.Platform.runLater(new Runnable() {
@SuppressWarnings({ "unchecked", "unused" })
public void run() {
createTray();
............................
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error: " + e.getLocalizedMessage(), "DOH! An error", JOptionPane.ERROR_MESSAGE);
}
}
What am I missing in order to get it to open the original window instead of creating a new one once I click the icon in the system tray?
java swing javafx java-8 system-tray
java swing javafx java-8 system-tray
edited Jan 2 at 17:43
StealthRT
asked Jan 2 at 17:05
StealthRTStealthRT
4,67227138257
4,67227138257
The system tray feature you’re using is part of AWT, so there’s no sense in performing its creation in the JavaFX thread. It’s also inconsistent to install the Windows look&feel but then setting Metal look&feel specific properties. Besides that, useUIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
instead of hardcoding a class name that is not part of the public API. But to your specific question, a call to.setVisible(true)
will never create a new window; if you see two windows then, you already created them before that event.
– Holger
Jan 7 at 16:56
add a comment |
The system tray feature you’re using is part of AWT, so there’s no sense in performing its creation in the JavaFX thread. It’s also inconsistent to install the Windows look&feel but then setting Metal look&feel specific properties. Besides that, useUIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
instead of hardcoding a class name that is not part of the public API. But to your specific question, a call to.setVisible(true)
will never create a new window; if you see two windows then, you already created them before that event.
– Holger
Jan 7 at 16:56
The system tray feature you’re using is part of AWT, so there’s no sense in performing its creation in the JavaFX thread. It’s also inconsistent to install the Windows look&feel but then setting Metal look&feel specific properties. Besides that, use
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
instead of hardcoding a class name that is not part of the public API. But to your specific question, a call to .setVisible(true)
will never create a new window; if you see two windows then, you already created them before that event.– Holger
Jan 7 at 16:56
The system tray feature you’re using is part of AWT, so there’s no sense in performing its creation in the JavaFX thread. It’s also inconsistent to install the Windows look&feel but then setting Metal look&feel specific properties. Besides that, use
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
instead of hardcoding a class name that is not part of the public API. But to your specific question, a call to .setVisible(true)
will never create a new window; if you see two windows then, you already created them before that event.– Holger
Jan 7 at 16:56
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010366%2fjava-system-tray-hiding-and-restoring-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010366%2fjava-system-tray-hiding-and-restoring-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The system tray feature you’re using is part of AWT, so there’s no sense in performing its creation in the JavaFX thread. It’s also inconsistent to install the Windows look&feel but then setting Metal look&feel specific properties. Besides that, use
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
instead of hardcoding a class name that is not part of the public API. But to your specific question, a call to.setVisible(true)
will never create a new window; if you see two windows then, you already created them before that event.– Holger
Jan 7 at 16:56