This discussion is archived
2 Replies Latest reply: Aug 19, 2013 12:49 AM by csh RSS

Implicit exit does not work, when you use AWT for SystemTray

csh Journeyer
Currently Being Moderated

Hi,

 

normally my JavaFX application exits the JVM automatically, when the last window is closed. (Platform.setImplicitExit(true) is the default)

 

But as soon as I use AWT for displaying a System Tray icon, it doesn't anymore.

Seems as if the AWT thread continues running.

 

Does anybody know how to do it?

 

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import java.awt.*;
import java.awt.image.BufferedImage;
public class TestApp5 extends Application {
    public static void main(String[] args) throws Exception {
        launch(args);
    }
    public void start(final Stage stage) throws Exception {
        Label root = new Label("test");
        SystemTray.getSystemTray().add(new TrayIcon(new BufferedImage(16, 16, BufferedImage.TYPE_3BYTE_BGR)));
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }
}
  • 1. Re: Implicit exit does not work, when you use AWT for SystemTray
    shakir.gusaroff Expert
    Currently Being Moderated

    If you use AWT in your application you have at least two threads.  The native part of AWT runs on one thread and the Java part runs on another one.  When you exit your Java part, the native part of AWT is still running.

    The javafx application without AWT has a single javafx application thread.

    If javafx had a system tray icon you would not have this issue.

    I suggest adding the following to resolve it.

    stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
     @Override
     public void handle(WindowEvent e) {
     System.exit(0);
     }
     });

    It works for me.

    I hope someday we will have system tray icon functionality in javafx8.

    You may want to vote for the javafx system tray api https://javafx-jira.kenai.com/browse/RT-24251

  • 2. Re: Implicit exit does not work, when you use AWT for SystemTray
    csh Journeyer
    Currently Being Moderated

    Yes that works of course.

     

    But I thought maybe there is something like

     

    AWTThread.cancel() or SwingUtilities.setImplicitExit(true).

     

    I also tried

     

    SwingUtilities.invokeLater(new Runnable(){

    Thread.currentThread.setDaemon(true);

    });

     

    It seems to work, but it throws an IllegalStateException nonetheless.

     

    Shutting down the JVM doesn't seem like the best option. Maybe you want other threads to finish their work.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points