4

I used a system tray in my java application. I want to disappear the GUI and run the application in background, but system tray must remain available when user click on close button of JFrame.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sal-laS
  • 11,016
  • 25
  • 99
  • 169
  • 1
    Don't set the frame to EXIT_ON_CLOSE or call System.exit when the frame is closed. The event dispatching thread will continue to run until the JVM is terminated – MadProgrammer Mar 15 '13 at 21:14
  • @MadProgrammer That's the right answer - in fact, why not post it as an answer? – Adrian Mar 15 '13 at 21:26
  • @adrian Cause its to early in the morning and I need another cup if tea :P – MadProgrammer Mar 15 '13 at 21:40
  • Please have a look at this related example, How to [hide JFrame in SystemTray of TaskBar](http://stackoverflow.com/q/7461477/1057230) – nIcE cOw Mar 16 '13 at 06:51

4 Answers4

5

I want to disappear the GUI and run the application in background, but system tray must remain available when user click on close button of JFrame.

Sets the operation that will happen by default when the user initiates a "close" on this frame. You must specify one of the following choices:

DO_NOTHING_ON_CLOSE (defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.

HIDE_ON_CLOSE (defined in WindowConstants): Automatically hide the frame after invoking any registered WindowListener objects.

DISPOSE_ON_CLOSE (defined in WindowConstants): Automatically hide and dispose the frame after invoking any registered WindowListener objects.

EXIT_ON_CLOSE (defined in JFrame): Exit the application using the System exit method. Use this only in applications.

The value is set to HIDE_ON_CLOSE by default. Changes to the value of this property cause the firing of a property change event, with property name "defaultCloseOperation".

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
1

Posting this as an answer

Just like MadProgrammer said:

Don't set the frame to EXIT_ON_CLOSE or call System.exit when the frame is closed. The event dispatching thread will continue to run until the JVM is terminated

0

You can have the System Tray available in a separate thread.

CodeBlue
  • 14,631
  • 33
  • 94
  • 132
  • That's a very dangerous suggestion – MadProgrammer Mar 15 '13 at 21:14
  • 1
    Even though TrayIcon is a AWT component, it still relies on and uses the Event Dispatching Thread. This could cause updates to UI to executed out side the context of the EDT. Better to put the none UI processing in a separate thread instead – MadProgrammer Mar 15 '13 at 21:36
  • What he said... I just spent a week troubleshooting defects caused by calling Swing methods from outside EDT. These things take forever to track down if you don't pay attention to it up front. – Adrian Mar 15 '13 at 21:48
-1

if I do not understand bad you want a background app with availability to see GUI in any moment:

  1. You can set your frame as HIDE_ON_CLOSE when you click 'X' button in your window

  2. You need create a code similar like this:

  3. Check that we have 2 buttons with different actions

     try {            
         Main_view frame = new Main_view();
         frame.setVisible(true);
    
         if (SystemTray.isSupported()) {
    
             SystemTray tray = SystemTray.getSystemTray();
             TrayIcon trayIcon = null;
    
             //Listener for exit button
             ActionListener ExitListener = new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.exit(0);
                }
              };
    
              //Listener for display button
              ActionListener DisplayListener = new ActionListener() {
                 public void actionPerformed(ActionEvent e) {
                     frame.setVisible(true);
                 }
              };
    
               //Menu when you right click the icon
               PopupMenu popup = new PopupMenu();
    
               //Buttons to show
               MenuItem displayButton = new MenuItem("Display");
               MenuItem exitButton = new MenuItem("Exit");
    
               //add the previous actions made it
               exitButton.addActionListener(ExitListener);
               displayButton.addActionListener(DisplayListener);
    
               //add to the popup
               popup.add(displayButton);
               popup.add(exitButton);
    
               // URL: MyProject/src/MyGUI/check.png 
               // Small icon on secondary taskbar
               Image image= ImageIO.read(getClass().getResource("/MyGUI/check.png"));
               trayIcon = new TrayIcon(image, "App name", popup);
               trayIcon.setImageAutoSize(true);
    
    
               trayIcon.addActionListener(DisplayListener);
               trayIcon.addActionListener(ExitListener);
    
               try {
                    tray.add(trayIcon);
    
                   } catch (AWTException e) {
    
                          System.err.println(e);
                      }
                      // ...
                  } else {
                      // disable tray option in your application or
                      // perform other actions
                  }
             } catch (Exception e) {
                 e.printStackTrace();
             }
    

Results

enter image description here

Heterocigoto
  • 183
  • 2
  • 11
  • Almost all the code was extracted from: https://docs.oracle.com/javase/6/docs/api/java/awt/SystemTray.html If you have a Java Name convention to share please do it, could be useful – Heterocigoto Oct 11 '22 at 19:35