Skip to content

JavaFXToaster IllegalStateException #6

@Ra4ster

Description

@Ra4ster

Howdy,

I'm using two-slices for desktop notifications and have a scenario that would benefit from knowing the toast's closed state explicitly.

In my program, I am creating a daily "notification" toast:

public class Notifier {
	
	public static Slice toast;
	
	public static void reminder(String title, String message, SceneManager sm) {
		URL url = Notifier.class.getClassLoader().getResource("rs/Resources/Geometric_Flowers.png");
		
		toast = Toast.builder().
		content(message)
		.action("Open", () -> sm.getStage().toFront())
		.action("Exit", () -> Platform.exit())
		.title("Chamomile")
		.icon(url)
		.defaultAction(() -> Platform.exit())
		.toast();
	}
	
	public static void closeToast() { 
		if (toast == null) return;
	    Platform.runLater(() -> {
            try {
                toast.close();
            } catch (IllegalStateException e) {
                System.out.println("No active toast to close. Ignore this.");
            } catch (Exception e) {
            	e.printStackTrace();
            }
	    });
	}
}

The problem occurs when the user clicks on the toast, causing it to fade out. My assumption is that close is called afterwards, but this is problematic as close must also be called by the implementer (me) at the end of program. The user fading it out and then closing the program calls close() twice.

To fix this, I could ignore the IllegalStateException, but this is bad for several notifications. That is why I'd like this Feature Request:

  • Add a boolean property like isClosed() or similar on the Slice or Toast interface/class that:
  • Starts as false when the toast is created.
  • Changes to true after the first close() (and internal close from fade-out!).
  • Can be called using isClosed() by the implementer to check if the toast is still active.

This enables implementers to safely call close() without risking exceptions or needing to ignore extra try-catch blocks.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions