How to Run application in background when close the JFrame?

泄露秘密 提交于 2021-01-27 05:28:35

问题


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.


回答1:


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




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/15441431/how-to-run-application-in-background-when-close-the-jframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!