Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Run application in background when close the JFrame?

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.

like image 617
Sal-laS Avatar asked Mar 15 '13 20:03

Sal-laS


2 Answers

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

like image 36
user2052598 Avatar answered Oct 09 '22 00:10

user2052598


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.

  • set proper JFrames method for DefaultCloseOperation, JFrame.setDefaultCloseOperation(HIDE_ON_CLOSE), by default implemented in API

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".

  • then from SystemTray to call JFrame.setVisible(true), this event is accesible from

    1. TrayIcon

    2. JPopupMenu added to SystemTray

like image 123
mKorbel Avatar answered Oct 08 '22 23:10

mKorbel