Search code examples
javaswingswingx

SwingX fading effect on JXFrame?


I tried the demo of the swingx framework (http://swingx.java.net/). When you close the main window you have a great fading effect. I tried to reach this effect with the JXFrame but it has just the normal closing effect.

Anyone has an idea?


Solution

  • thanks :-)

    It's not related to JXFrame (could be done with any Window), just a timeline bound to the window's opacity property. This timeline is started when the application window is closed, the relevant code is in DemoUtils

    public static void fadeOutAndDispose(final Window window,
            int fadeOutDuration) {
        fadeOutAndEnd(window, fadeOutDuration, false);
    }
    
    public static void fadeOutAndExit(Window window, int fadeOutDuration) {
        fadeOutAndEnd(window, fadeOutDuration, true);
    }
    
    private static void fadeOutAndEnd(final Window window, int fadeOutDuration, 
            final boolean exit) {
        Timeline dispose = new Timeline(new WindowFader(window));
        dispose.addPropertyToInterpolate("opacity", 1.0f,
                0.0f);
        dispose.addCallback(new UIThreadTimelineCallbackAdapter() {
            @Override
            public void onTimelineStateChanged(TimelineState oldState,
                    TimelineState newState, float durationFraction,
                    float timelinePosition) {
                if (newState == TimelineState.DONE) {
                    if (exit) {
                        Runtime.getRuntime().exit(0);
                    } else {
                        window.dispose();
                    }
                }
            }
        });
        dispose.setDuration(fadeOutDuration);
        dispose.play();
    }
    
    public static class WindowFader {
        private Window window;
    
        public WindowFader(Window window) {
            this.window = window;
        }
    
        public void setOpacity(float opacity) {
            AWTUtilitiesWrapper.setWindowOpacity(window, opacity);
        }
    }
    

    Note that the fading effect will no longer work for all frames in jdk7, as the behaviour was changed to effect only undecorated windows

    Edit

    Timeline (and the callbackAdapter) are classes in Trident, one of Kirill's projects, unfortunately abandoned by him, taken over as Insubstantial (don't have a reference handy, sorry) - but you can take any other or handcode a Timer