Hide Java application to System Tray - java

I want to hide my Java application from the taskbar and for it to be visible only at the system tray. Here is a picture in case this is unclear.
I tried to implement it like this and the icon did appear in the system tray but it is still showing the application on the taskbar.
Here is part of the Frame class
public class Widget extends JFrame {
private static final long serialVersionUID = -197136854089859547L;
private JPanel mainPanel;
private WidgetProperties properties;
private Makedir mkdir;
private ArrayList<Item> items;
private Image icon;
private TrayIcon trayIcon;
private SystemTray tray;
public Widget() {
super("");
this.setTray();
this.setUndecorated(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void setTray() {
tray = SystemTray.getSystemTray();
PopupMenu menu = new PopupMenu();
MenuItem show = new MenuItem("Show");
MenuItem exit = new MenuItem("Exit");
show.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
setState(Frame.NORMAL);
}
});
exit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
close();
}
});
menu.add(show);
menu.addSeparator();
menu.add(exit);
trayIcon = new TrayIcon(icon, "EasyStart", menu);
trayIcon.setImageAutoSize(true);
try
{
tray.add(trayIcon);
} catch (AWTException e) {
e.printStackTrace();
}
}
public void setup() {
this.resize();
this.setVisible(true);
}
public void resize() {
this.setResizable(true);
this.setShape(properties.getShape());
this.setSize(properties.getSize());
this.setResizable(false);
}
public void close() {
System.exit(0);
}
}
I just need to find how to hide the application from the taskbar.

do this setVisible(false) then there will be nothing in the taskbar

Related

JCheckBox State Remain Consistent Among Classes

I have a help pane which appears at the start of a program, but can be turned off. If the user wants it to return, there is an option in the menu bar to reactivate it. However, when they choose to show it from the help menu, it automatically rechecks the "do not show again" box. How do I keep the box in the same state the user originally had it, but still open the help pane?
Gui:
public class Gui {
private Game game;
private JFrame frame;
private MenuBar menuBar;
private HelpDialog helpMenu;
private boolean showHelp;
public Gui(Game game) {
this.game = game;
this.showHelp = true;
this.createAndShowGUI();
}
public boolean shouldShowHelpDialog() {
return this.showHelp;
}
public void displayHelp() {
this.helpMenu.showHelpDialog();
}
MenuBar:
public class MenuBar {
private JMenuBar menuBar;
private JMenu menu;
private JMenuItem menuItem;
private JFrame frame;
private Gui gui;
private Game game;
public MenuBar(JFrame frame, Gui gui, Game game) {
this.menuBar = new JMenuBar();
this.frame = frame;
this.gui = gui;
this.game = game;
}
public void buildMenuBar() {
this.buildFileMenu();
this.buildSettingsMenu();
this.buildHelpMenu();
this.frame.setJMenuBar(this.menuBar);
}
private void buildHelpMenu() {
this.menu = new JMenu("Information");
this.menu.setMnemonic(KeyEvent.VK_I);
this.menu.getAccessibleContext().setAccessibleDescription("Help menu");
JMenuItem menuHelp = new JMenuItem("Help", KeyEvent.VK_H);
menuHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
MenuBar.this.gui.displayHelp();
}
});
this.menu.add(menuHelp);
this.menuBar.add(this.menu);
}
HelpDialog:
public class HelpDialog {
private boolean shouldShowHelpDialog;
private JFrame theFrame;
public HelpDialog(boolean helpDialog, JFrame frame) {
this.shouldShowHelpDialog = helpDialog;
this.theFrame = frame;
}
public boolean showHelpDialog() {
if (!this.shouldShowHelpDialog) {
return false;
}
JCheckBox shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);
Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };
JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);
return shouldShowCheckBox.isSelected();
}
private Object buildHelpPane() {
String helpMessage = "Game rules: This is how you play.";
JTextArea helpTextArea = new JTextArea(helpMessage);
helpTextArea.setRows(6);
helpTextArea.setColumns(40);
helpTextArea.setLineWrap(true);
helpTextArea.setWrapStyleWord(true);
helpTextArea.setEditable(false);
helpTextArea.setOpaque(false);
JScrollPane helpPane = new JScrollPane(helpTextArea);
return helpPane;
}
}
EDIT:
Updated HelpDialog class:
public class HelpDialog {
private boolean shouldShowHelpDialog;
private JFrame theFrame;
private JCheckBox shouldShowCheckBox;
public HelpDialog(boolean helpDialog, JFrame frame) {
this.shouldShowHelpDialog = helpDialog;
this.theFrame = frame;
this.shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);
}
public boolean showHelpDialog() {
if (!this.shouldShowHelpDialog) {
return false;
}
Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };
JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);
return shouldShowCheckBox.isSelected();
}
The checkbox remains unmarked now when displaying the help menu through the menu bar. However, now when a new game is created, it will show the help dialog even if the box is unchecked.
Full answer includes this change to the method in the GUI:
public void displayHelp() {
this.showHelp = this.helpMenu.showHelpDialog();
}
Your showHelpDialog() method creates a new checkbox each time it is called. You should create the dialog once in the constructor, and showHelpDialog() should just display it.
You can add a parameter to showHelpDialog which overrides your request
public boolean showHelpDialog(boolean override) {
if(!override){
if (!this.shouldShowHelpDialog) {
return false;
}
}
JCheckBox shouldShowCheckBox = new JCheckBox("Do not show this message again", this.shouldShowHelpDialog);
Object[] msgContent = { this.buildHelpPane(), shouldShowCheckBox };
JOptionPane.showMessageDialog(this.theFrame, msgContent, "Help", JOptionPane.INFORMATION_MESSAGE);
return shouldShowCheckBox.isSelected();
}
and call
showHelpDialog(true);
when clicked from menu.

java's SystemTray not working on Gnome 3.14

I'm trying to put a Java SystemTray icon on gnome's notification panel (I'm using OpenSuse 13.2, that uses gnome 3.14).
Its not working, although SystemTray.isSupported() is returning "true". I'm not seeing any icon on the screen. I was expecting it to appear next to OpenSuse's notification area.
This is the main's code:
public static void main(String[] args) {
//checking for support
if (!SystemTray.isSupported()) {
System.out.println("System tray is not supported !!! ");
return;
}
SystemTray systemTray = SystemTray.getSystemTray();
Image image = Toolkit.getDefaultToolkit().getImage("icon.ico");
//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

Extension of JDialog (hidden?) not showing up in front of parent JFrame?

I have an application I'm making for a game to automatically update a game client.
Once you press Launch, it will open up my DownloadFrame (extends JDialog), and will look like this:
If you click the icon for the application in the taskbar, (maybe Windows 8 is the problem?) it will minimize the application like usual. However when you go to maximise the application again, the JDialog will be hidden, I'm assuming, behind the parent. It looks like this:
Here's my code for my extension of JDialog. Apologies in advance for it being messy.
public class DownloadFrame extends JDialog implements Runnable {
private static final long serialVersionUID = -8764984599528942303L;
private Background frame;
private ImageIcon[] gifs;
private JLabel spinner;
public DownloadFrame() {
super(Loader.application, false);
setLayout(null);
setUndecorated(true);
setAutoRequestFocus(true);
new Thread(this).start();
generateBackground();
generateButton();
generateGif();
}
private void generateBackground() {
frame = new Background("sub_background.png");
setSize(frame.getWidth(), frame.getHeight());
setBackground(new Color(1.0f, 1.0f, 1.0f, 0.0f));
setLocationRelativeTo(null);
setLocation(this.getX(), this.getY() + 5);
setLayout(null);
setContentPane(frame);
}
private void generateGif() {
gifs = Utils.generateGifImages();
spinner = new JLabel(gifs[0]);
spinner.setBounds(70, 30, gifs[0].getIconWidth(), gifs[0].getIconHeight());
add(spinner);
}
private HoverableButton cancel;
public HoverableButton getCancelButton() {
return cancel;
}
private void generateButton() {
cancel = new HoverableButton(Settings.CANCEL_BUTTON, 75, 145);
cancel.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
/*
* TODO -
* stop the download in progress
*/
for (HoverableButton button : Loader.application.getPrimaryButtons()) {
button.setActive(true);
button.setVisible(true);
}
dispose();
}
});
add(cancel);
}
private int cycleCount;
private void cycleGif() {
if (spinner == null) {
return;
}
cycleCount++;
if (cycleCount > 7) {
cycleCount = 0;
}
spinner.setIcon(gifs[cycleCount]);
}
#Override
public void run() {
while (true) {
cycleGif();
try {
Thread.sleep(100L);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
In case it's needed, here's my usage of it. Most of the stuff can be ignored I'm sure, it's simply there to hide the four buttons while the download is in progress.
((HoverableButton) components[2]).addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
HoverableButton source = (HoverableButton) components[2];
if (source.isActive()) {
try {
Thread.sleep(500L);
} catch (Exception ex) {
ex.printStackTrace();
}
if (panel == null) {
panel = new DownloadFrame();
panel.setVisible(true);
} else {
panel.setVisible(true);
panel.getCancelButton().removeHighlight();
}
for (HoverableButton button : getPrimaryButtons()) {
button.setActive(false);
button.setVisible(false);
button.removeHighlight();
}
/*
* TODO -
* handle checking for updates / downloading updates
*/
}
}
});
However when you go to maximise the application again, the JDialog will be hidden, I'm assuming, behind the parent
Yes. When you create the JDialog, you need to specify the "owner" JFrame of the dialog in the constructor.
So you must create and make the JFrame and make the frame visible before you create the dialog.

Java in background of windows for copying text from the browser directly to text file

Is it possible to listen to events from browser with Java?
The main task is to add command "copy to file" to pop-up menu of right click of the mouse. This command must add selected text in browser, in Notepad, in winword (any selectable text) to specific text file.
I've just tried code which adds icon to tray but I do not know whether it can it be developed for solving my task.
import java.awt.*;
import java.awt.event.*;
public class SystemTrayTest
{
public SystemTrayTest()
{
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 Peformed!",
TrayIcon.MessageType.INFO);
}
};
trayIcon.setImageAutoSize(true);
trayIcon.addActionListener(actionListener);
trayIcon.addMouseListener(mouseListener);
// Depending on which Mustang build you have, you may need to uncomment
// out the following code to check for an AWTException when you add
// an image to the system tray.
// try {
tray.add(trayIcon);
// } catch (AWTException e) {
// System.err.println("TrayIcon could not be added.");
// }
} else {
System.err.println("System tray is currently not supported.");
}
}
/**
* #param args the command line arguments
*/
public static void main(String[] args)
{
SystemTrayTest main = new SystemTrayTest();
}
}
You are talking about accessing clipboard events. this may helps you. How do we get notified about system clipboard events?

JPopupMenu Behavior OSX 10.6.7

My problem is when I right click on the JFrame in the example below, the JPopupMenu shows up but if I click anywhere outside the JFrame, the menu does not disappear. I have to click somewhere on the JFrame to get rid of it which is not the expected behavior. Here are the steps to reproduce:
Run the window class from Eclipse (JFrame appears)
Click into the Eclipse workspace (JFrame loses focus and is hidden behind eclipse)
Minimize Eclipse (JFrame appears)
Go with your mouse over JFrame and make a right click (Popup appears)
Click somewhere (Not into JFrame or Popup). The Popup will not disappear
I'm running OS X 10.6.7 and Java full version 1.6.0_24-b07-334
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class test
{
static class window extends JFrame implements MouseListener,
MouseMotionListener
{
JPopupMenu popMenu;
JPanel panel = new JPanel();
Point location;
MouseEvent pressed;
public window()
{
addMouseListener(this);
addMouseMotionListener(this);
JLabel label = new JLabel("JFrame", JLabel.CENTER);
initPopMenu();
add(label);
setUndecorated(true);
setVisible(true);
// setAlwaysOnTop(true);
setLocationRelativeTo(null);
pack();
}
public void initPopMenu()
{
popMenu = new JPopupMenu();
JMenuItem item;
item = new JMenuItem("Title");
item.setEnabled(false);
popMenu.add(item);
popMenu.addSeparator();
item = new JMenuItem("Item One");
popMenu.add(item);
item = new JMenuItem("Item 2");
popMenu.add(item);
item = new JMenuItem("Item 3");
popMenu.add(item);
}
public void mousePressed(MouseEvent e)
{
pressed = e;
int nModifier = e.getModifiers();
if (((nModifier & InputEvent.BUTTON2_MASK) != 0)
|| ((nModifier & InputEvent.BUTTON3_MASK) != 0))
popMenu.show( this, e.getX(), e.getY() );
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseDragged(MouseEvent me)
{
}
public void mouseMoved(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
public static void main(String[] args)
{
window dw = new window();
}
}
you can add a windowFocusListener, hide the menu when window lost focus
this.addWindowFocusListener(new WindowFocusListener() {
#Override
public void windowLostFocus(WindowEvent e) {
if(popMenu != null){
popMenu.setVisible(false);
}
}
#Override
public void windowGainedFocus(WindowEvent e) {
//System.out.println(e);
}
});

Categories