Skip to content
Advertisement

How can I fade out or fade in by command JPanel, its components and its color

I wanted to make a Glass Panel that contain a JPanel with white background, border and the msg “please wait”.

Here is the code example:

JavaScript

It will fade in and out while waiting for the query. the problem is that I am getting a bad result.

need help

Advertisement

Answer

the other is that none of them show it with glass panel

Animating the opacity state of a glassPane is no different from animating the state of any Swing component, after all, the glassPane is just another component.

one is that the Timer system doesn’t know if the start function started and it keeps the panel hanging on because it closing it before fading the panel and then before it shows it and then it dont try to close it again

This is more about your own internal state management. The panel shouldn’t care, it should just be responding to the request to change opacity level, forward or backwards

What you should have, is some kind of “engine” which can provide events when certain states are achieved, at which time, you make decisions about what should be done, removing the functionality from the “panel” itself.

Theory TL;DR

Okay, first, some theory.

Animation…

Animation is the illusion of change over time. In your case, you’re moving from 0 to 1 and back again over a specified period of time. This is commonly known as “linear progression/animation”. Most naive animation implementations will simple add a constant delta to a value and keep doing so until a desired state is reached. This is naive because not all systems are equal. Some will be able to achieve the desired state faster than others, making the animation uneven and providing a poor user experience.

Instead, you should be focused on perform a operation over a fixed period of time, calculating the required value as fast as the system will allow. This allows the animation to “drop” frames as required based on the system’s capabilities. This is commonly known as “duration based animation”.

This approach is much more powerful, as it allows you to play around with the speed of the animation in a very simply way. It also allows you do some very advanced operations, like easement, which wouldn’t be easily achievable through a linear progression.

Swing and animation…

Swing is SINGLE threaded. This means you can’t perform blocking or long running operations within the context of the Event Dispatching Thread.

Swing is also NOT thread safe. This means you shouldn’t update the UI (or any state the UI depends on) from outside the context of the EDT.

For animation, what you need is some way to post, fast, repetitive, events onto the EDT, which will allow you to make changes to the UI safely. For this, the most common tool is a Swing Timer

The Framework

So based on that, what we need is some kind of “engine”, which given a “range” and a “duration” can notify us of “ticks” on a regular bases from which we can calculate the progression that the animation has played, and calculate the value we should use based on our inputs … simple …

I, personally, prefer to use an animation library, but the simple framework presented in the examples basically abstracts all these concepts into a re-usable framework.

Make it so…

nb: I ran out of room, so the underlying framework is included in the main example

Okay, that’s all nice and fluffy, but how does this actually help us. Essentially, the idea of the above is to abstract common functionality out and make it re-usable (and yes, I actually do use it, a lot)

What we now need, is a component which can actually use it, something like…

JavaScript

Okay, this is a pretty simple concept. It’s a JPanel which has a alpha property which changes the opacity level of the component – basically, this is all faked, as Swing only support opaque and transparent components, not translucent components. So we set the component to be transparent and manually paint the background ourselves.

The component exposes two methods, fadeIn and fadeOut and supports a FaderListener which can be used to notify interested parties that the fade operation has been completed

Runnable example…

Fade

JavaScript

But it’s not a glassPane

… ok, as I said, a glassPane is just another component

This is a simple example which makes use of the frame’s glassPane and will, when the panel is faded out, reset the glassPane to a default component

JavaScript

nb: The required classes are in the previous example

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement